[英]AutoCompleteTextView detect when selected entry from list edited by user
I have an AutoCompleteTextView
I use to select an item from a long list. 我有一个
AutoCompleteTextView
我用来从长列表中选择一个项目。 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. 我检查以确保他们只提交列表中的项目的方法是使用
setOnItemClickListener
来触发布尔标志。 The problem is that after the boolean flag is set to true, they can still edit the selected text of the item. 问题是在布尔标志设置为true后,他们仍然可以编辑项目的选定文本。 I need to detect this and set the boolean flag to false again.
我需要检测到这一点并再次将布尔标志设置为false。 How do I do this.
我该怎么做呢。 I have seen a suggestion to use
onKeyDown
, but I am not sure how to implement this. 我已经看到了使用
onKeyDown
的建议,但我不知道如何实现它。
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) {
}
});
Implement a TextWatcher
, which will give you 3 methods which will constantly get call backs when someone changes the text. 实现
TextWatcher
,它将为您提供3种方法,当有人更改文本时,这些方法将不断获得TextWatcher
。 If the string grows, your user is typing by himself again. 如果字符串增长,您的用户将再次自己键入。
Use 使用
AutoCompleteTextView#setOnItemSelectedListener()
- works like a charm. - 奇迹般有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.