用户从下拉菜单中选择项目后禁用 Android AutoCompleteTextView [英] Disable Android AutoCompleteTextView after user selects item from drop down

查看:37
本文介绍了用户从下拉菜单中选择项目后禁用 Android AutoCompleteTextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Android 的 AutoCompleteTextViewCursorAdapter 为应用添加自动完成功能.在视图的 onItemClickListener()(即当用户触摸自动完成的下拉项目之一时)中,我检索文本并将其放置在 EditText 中,以便用户可以在需要时对其进行修改.

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.

但是,当我在 TextView 上调用 setText() 时,会触发自动完成行为并再次显示下拉列表.如果用户使用键盘输入新文本,我只想显示下拉列表.有没有办法做到这一点?

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?

推荐答案

在经过几个小时的黑客攻击后回答我自己的问题:事实证明你应该实现自己的 OnItemClickListener 而不是依赖现有的点击侦听器来填充 TextView.我最初实现了 onItemClickListener,因为它使用 Cursor.toString() 的结果来填充文本视图.要更改输出字符串,您应该在 CursorAdapter 中实现 convertToString(Cursor).返回的 CharSequence 将填充在文本视图中.

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.

这样做还会阻止下拉菜单再次显示(因为 setText() 会触发完成行为,但默认的 onItemClickListener 不会).

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

这篇关于用户从下拉菜单中选择项目后禁用 Android AutoCompleteTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆