How to disable AutoCompleteTextView's drop-down from showing up?

前端 未结 17 1976
终归单人心
终归单人心 2020-12-05 15:30

I use the following code to set text to an AutoCompleteTextView field. But I noticed that when I set certain text (not all text, but some) to it, it will automatically pop u

17条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 16:09

    Maybe it is to late, but I've found elegant solution for this problem:

    Disable filtering before setting text and enabling it after (instead of playing with focus or/and delays). You should use custom control in this case.

    See example below:

    public class CustomCompliteTextView extends AutoCompleteTextView {
    
        private boolean mIsSearchEnabled = true;
    
        public CustomCompliteTextView(Context context) {
            super(context);
        }
    
        public CustomCompliteTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomCompliteTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public void setSearchEnabled(boolean isEnabled) {
            mIsSearchEnabled = isEnabled;
        }
    
        @Override
        protected void performFiltering(CharSequence text, int keyCode) {
            if (mIsSearchEnabled) {
                super.performFiltering(text, keyCode);
            }
        }
    }
    

    And usage:

        text.setSearchEnabled(false);
        text.setText("Text you want to set");
        // optional, if you also want to set correct selection
        text.setSelection(text.getText().length());
        text.setSearchEnabled(true);
    

提交回复
热议问题