How can I avoid autocomplete dropdown appearing when text is programmatically set?

后端 未结 5 1753
说谎
说谎 2021-02-07 04:16

I have an AutoCompleteTextView in my layout. I also have an alternative way to select the same items which are present in the AutoCompleteTextView. When the alternative way is

5条回答
  •  猫巷女王i
    2021-02-07 04:52

    If you want to support API<17, Subclass AutoCompleteTextview and override setText(text, filter) method

    @Override
    public void setText(CharSequence text, boolean filter) {
        if(Build.VERSION.SDK_INT>=17) {
            super.setText(text, filter);
        }else{
            if(filter){
                setText(text);
            }else{
                ListAdapter adapter = getAdapter();
                setAdapter(null);
                setText(text);
                if(adapter instanceof ArrayAdapter)
                    setAdapter((ArrayAdapter) adapter);
                else
                    setAdapter((CursorAdapter) adapter);
                //if you use more types of Adapter you can list them here
            }
        }
    }
    

    Then whenever you want to set the text manually call setText(text, false)

提交回复
热议问题