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

前端 未结 17 1970
终归单人心
终归单人心 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 15:50

    You can try these steps:

    1. When setting the text, also set the Threshold value to a large value so that the dropdown doesnot come.

       actv.setThreshold(1000);
      
    2. Then override the OnTouch to set the threshold back to 1.

         actv.setOnTouchListener(new OnTouchListener() {
                      @Override
          public boolean onTouch(View v, MotionEvent event) {
              actv.setThreshold(1);
              return false;
          }
      });
      
    0 讨论(0)
  • 2020-12-05 15:50

    Funny trick. Works 100% :)

    tv.setThreshold(Integer.MAX_VALUE);
    tv.setText(station.getName());
    tv.setThreshold(1);
    
    0 讨论(0)
  • 2020-12-05 15:51

    Another solution is to clear the focus before setting the text:

    mContactTxt.setFocusable(false);
    mContactTxt.setFocusableInTouchMode(false);
    mContactTxt.setText("");            
    mContactTxt.setFocusable(true);
    mContactTxt.setFocusableInTouchMode(true);
    
    0 讨论(0)
  • 2020-12-05 15:51
    setText("someText",false)
    

    false means that it is not filtering

    0 讨论(0)
  • 2020-12-05 15:51

    dismissDropDown() works well in an adapter:

            SimpleCursorAdapter autoCompleteAdapter = new SimpleCursorAdapter(this,
                        android.R.layout.select_dialog_item, null,
                        new String[] { ContactsContract.Contacts.DISPLAY_NAME },
                        new int[] { android.R.id.text1 }, 0);
            mSearchView.setAdapter(autoCompleteAdapter); 
            autoCompleteAdapter.setFilterQueryProvider(new FilterQueryProvider() {
                @Override
                public Cursor runQuery(CharSequence constraint) {
                   mSearchView.dismissDropDown();
                   // return your query code
                }
            });
    

    Hope it will be helpful.

    0 讨论(0)
  • 2020-12-05 15:52

    I have solved the same problem with this code:

    contacts_search.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub
                    contacts_search.dismissDropDown();      
                }
    
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub
                    contacts_search.dismissDropDown();
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    contacts_search.dismissDropDown();
                }
            });
    

    Here, contacts_search is my AutoCompleteTextView

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