Disable Android AutoCompleteTextView after user selects item from drop down

前端 未结 5 1057
北海茫月
北海茫月 2021-02-07 06:47

I\'m using Android\'s AutoCompleteTextView with a CursorAdapter to add autocomplete to an app. In the view\'s onItemClickListener() (i.e.

相关标签:
5条回答
  • 2021-02-07 07:31

    You can use the dismissDropDown() method of the AutoCompleteTextView object. Take a look at the documentation.

    0 讨论(0)
  • 2021-02-07 07:33

    Different approach. I agreed dismissDropDown() works but in my case, it wasn't working as expected. So, I used:

    autoCompleteTextView.setDropDownHeight(0);
    

    And if you want to show the dropdown list again, you an use

    autoCompleteTextView.setDropDownHeight(intValue);
    
    0 讨论(0)
  • 2021-02-07 07:45

    If you wish to dissmis AutoCompleteTextView's dropdown you should use its post(Runnable r) method. It works for me :)

    Here is an example:

    mAutoCompleteTextView.post(new Runnable() {
        public void run() {
            mAutoCompleteTextView.dismissDropDown();
        }
    }
    
    0 讨论(0)
  • 2021-02-07 07:47

    Answering my own question after a couple hours of hacking at this: It turns out you should implement your own OnItemClickListener and instead rely on the existing click listener to populate the TextView. I had originally implemented the onItemClickListener because it was using the results of Cursor.toString() to populate the text view. To change the output String, you should implement convertToString(Cursor) in your CursorAdapter. The CharSequence that gets returned will be populated in the text view.

    Doing this will also prevent the dropdown from showing up again (since setText() triggers the completion behavior but the default onItemClickListener does not).

    0 讨论(0)
  • 2021-02-07 07:50

    When we click on item suggested in AutoCompleteTextView.onTextChanged() is performed before onItemClick So, to avoid this try below code..

    autocompletetextview.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
        }
    
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (autocompletetextview.isPerformingCompletion()) {
                // An item has been selected from the list. Ignore.
            } else {
                // Perform your task here... Like calling web service, Reading data from SQLite database, etc...
            }
        }
    
        @Override
        public void afterTextChanged(final Editable editable) {
    
        }
    });
    
    0 讨论(0)
提交回复
热议问题