简体   繁体   English

AutoCompleteTextView检测用户编辑的列表中的选定条目

[英]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.

相关问题 测试用户是否从 AutoCompleteTextView 中选择了一个项目 - Test if a user has selected an item from AutoCompleteTextView AutoCompleteTextView检测用户是否选择了建议 - AutoCompleteTextView detect if user choosed suggestion 第一次选择一个项目时,AutoCompleteTextView下拉列表没有隐藏 - AutoCompleteTextView drop down list is not hiding when an item selected for the first time 从 Firebase 设置 AutoCompleteTextView 材质下拉列表的选定值 - Setting selected value of AutoCompleteTextView Material drop down list from Firebase 在单击ImageView时检测用户选择的键 - detect the key selected by user when clicked on ImageView 当用户在AutoCompleteTextView中选择了一个选项时,如何以编程方式关闭Android键盘? - How do I close the Android keyboard programmatically when user has selected an option in AutoCompleteTextView? 选择项目后无法从android AutoCompleteTextView获取用户输入的文本 - Can't get user entered text from android AutoCompleteTextView after item selected AutoCompleteTextView:检测何时关闭下拉菜单并且未选择项目 - AutoCompleteTextView: detecting when dropdown is dismissed and item is NOT selected Android:从AutoCompleteTextview列出位置 - Android: List position from AutoCompleteTextview 从列表中选择时显示用户的相应详细信息 - Display corresponding details of user when selected from list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM