AutoCompleteTextView detect when selected entry from list edited by user

最后都变了- 提交于 2019-11-29 17:03:00

问题


I have an AutoCompleteTextView I use to select an item from a long list. The user should only be able to select a predetermined item from the list. They should not be able to enter their own item.

The way I check to make sure they submit only an item from the list is to use setOnItemClickListener to trigger a boolean flag. The problem is that after the boolean flag is set to true, they can still edit the selected text of the item. I need to detect this and set the boolean flag to false again. How do I do this. I have seen a suggestion to use onKeyDown, but I am not sure how to implement this.


回答1:


You can add text changed listener:

autoCompleteTextView.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {                

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});



回答2:


Implement a TextWatcher, which will give you 3 methods which will constantly get call backs when someone changes the text. If the string grows, your user is typing by himself again.




回答3:


Use

AutoCompleteTextView#setOnItemSelectedListener() 

- works like a charm.



来源:https://stackoverflow.com/questions/15356193/autocompletetextview-detect-when-selected-entry-from-list-edited-by-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!