-->

How to restrict AutoCompleteTextView dropdown dism

2019-06-16 18:50发布

问题:

I am working on a AutoCompleteTextView . I get some results when the users type in the AutoCompleteTextView and these are mandatory to select. But the problem is the drop down automatically dismisses when clicking on anywhere in the srceen. I want to avoid this. Is there any way I can achieve this.

Thanks.

回答1:

try the code below.

I am using the the AutoCompleteText to auto complete the location where the user is currently in, the locationList is nothing but an array that i wrote in the strings.xml file, so use your own string array here.

  locationList = res.getStringArray(R.array.ticketLocation);

        ArrayAdapter<String> locationAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, locationList);

        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries);
        textView.setThreshold(1);
        textView.setAdapter(locationAdapter);
        textView.setValidator(new Validator());
        textView.setOnFocusChangeListener(new FocusListener());

        textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                TextView ticketLocation = (TextView) view;
                getTicketLocation = ticketLocation.getText().toString();
            }
        });

and below is the code for validating the text input in the location field, the fixText() method prevent the user from typing text that does not exist in your string array, for instance: if the user type "germany" which does not exist in your string array list, it will be replaced by " " which is an empty string inside your edittext input field

 class Validator implements AutoCompleteTextView.Validator {

        @Override
        public boolean isValid(CharSequence text) {
            // Log.v("Test", "Checking if valid: " + text);
            Arrays.sort(locationList);
            if (Arrays.binarySearch(locationList, text.toString()) > 0) {
                return true;
            }

            return false;
        }

        @Override
        public CharSequence fixText(CharSequence invalidText) {
            // Log.v("Test", "Returning fixed text");

            /*
             * I'm just returning an empty string here, so the field will be
             * blanked, but you could put any kind of action here, like popping
             * up a dialog?
             *
             * Whatever value you return here must be in the list of valid
             * words.
             */
            return "";
        }
    }

    class FocusListener implements View.OnFocusChangeListener {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // Log.v("Test", "Focus changed");
            if (v.getId() == R.id.txtCountries && !hasFocus) {
                // Log.v("Test", "Performing validation");
                ((AutoCompleteTextView) v).performValidation();
            }
        }
    }


回答2:

private boolean setForceIgnoreOutsideTouchWithReflexion(boolean forceIgnoreOutsideTouch) {
    try {
        Method method = android.widget.AutoCompleteTextView.class.getMethod("setForceIgnoreOutsideTouch", boolean.class);
        method.invoke(this, forceIgnoreOutsideTouch);
        return true;
    } catch (Exception e) {
        return false;
    }
}

Only reflexion in public class CustomAutoCompleteTextView extends AutoCompleteTextView, but - maybe this not a good solution too