用户从下拉列表中选择项目后禁用 Android AutoCompleteTextView
我使用 Android 的 AutoCompleteTextView 和 CursorAdapter 向应用程序添加自动完成功能。在视图的 onItemClickListener() 中(即当用户触摸自动完成的下拉项之一时),我检索文本并将其放置在 EditText 中,以便用户可以在需要时对其进行修改。
但是,当我在 TextView 上调用 setText()
时,会触发自动完成行为并再次显示下拉列表。我只想在用户使用键盘输入新文本时显示下拉列表。有办法做到这一点吗?
I'm using Android's AutoCompleteTextView
with a CursorAdapter
to add autocomplete to an app. In the view's onItemClickListener()
(i.e. when the user touches one of the autocompleted drop down items) I retrieve the text and place it in the EditText so that the user can modify it if they need to.
However, when I call setText()
on the TextView the autocomplete behavior is triggered and the dropdown shows again. I'd like to only show the dropdown if the user types new text with the keyboard. Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不同的方法。
我同意
dismissDropDown()
有效,但就我而言,它没有按预期工作。所以,我用了:如果你想再次显示下拉列表,你可以使用
Different approach.
I agreed
dismissDropDown()
works but in my case, it wasn't working as expected. So, I used:And if you want to show the dropdown list again, you an use
经过几个小时的黑客攻击后回答我自己的问题:事实证明,您应该实现自己的 OnItemClickListener ,而不是依赖现有的点击侦听器来填充 TextView。我最初实现了 onItemClickListener,因为它使用 Cursor.toString() 的结果来填充文本视图。要更改输出字符串,您应该在 CursorAdapter 中实现
convertToString(Cursor)
。返回的 CharSequence 将填充在文本视图中。这样做还将防止下拉列表再次显示(因为 setText() 会触发完成行为,但默认的 onItemClickListener 不会)。
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 implementconvertToString(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).
如果您想关闭 AutoCompleteTextView 的下拉列表,您应该使用它的 post(Runnable r) 方法。它对我有用:)
这是一个例子:
If you wish to dissmis AutoCompleteTextView's dropdown you should use its post(Runnable r) method. It works for me :)
Here is an example:
当我们点击
AutoCompleteTextView.onTextChanged()
中建议的项目时,会在onItemClick
之前执行因此,为了避免这种情况,请尝试下面的代码。
When we click on item suggested in
AutoCompleteTextView.onTextChanged()
is performed beforeonItemClick
So, to avoid this try below code..
您可以使用
dismissDropDown()
AutoCompleteTextView 对象的方法。查看文档。You can use the
dismissDropDown()
method of the AutoCompleteTextView object. Take a look at the documentation.