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

前端 未结 17 1974
终归单人心
终归单人心 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:04

    You can also enable/disable the drop-down like so:

    // disable
    actv.setDropDownHeight(0);
    // enable
    actv.setDropDownHeight(LayoutParams.WRAP_CONTENT);
    
    0 讨论(0)
  • 2020-12-05 16:04

    You can try

    searchInput.setAdapter((ArrayAdapter<String>) null);
    searchInput.setText(text);
    searchInput.setAdapter(adapter);
    

    Source

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • I need to add some delay to make this thing work.

    This worked for me:

    new Handler().postDelayed(new Runnable() {
        public void run() {
            if(carModel.isPopupShowing())
                carModel.dismissDropDown();
    }}, 500);
    
    0 讨论(0)
  • 2020-12-05 16:14

    I too faced a scenario and I resolved in this way.

    1. Store the values that you want to show, in a static way (Ex.POJO).
    2. Check whether the stored static variable is not null and not empty.
    3. if, its not empty / not null / its length greater than 0, set dismissDropDown() for that autocompleteTextView.

    Please find the below snippet

    if(null != testText && testText.length() != 0) {
        mAutoCompleteSearch.setText(incomingActivity.toString());
        mAutoCompleteSearch.dismissDropDown(); //Dismiss the drop down
        } else {
        mAutoCompleteSearchDocketActivity.setText("");
                // Here it(drop down) will be shown automatically
        }
    

    Hope, this would help for someone, Cheers !

    0 讨论(0)
提交回复
热议问题