Android:带有默认建议的 AutoCompleteTextView
在用户输入任何内容之前,如何显示 AutoCompleteTextView 的一些默认建议?即使创建扩展 AutoCompleteTextView 的自定义类,我也找不到方法来执行此操作。
我想显示常见输入值的建议,以节省用户打字的时间。
有什么建议吗?
How do I show some default suggestions for AutoCompleteTextView before the user type anything? I cannot find a way to do this even with creating a custom class that extends AutoCompleteTextView.
I want to show suggestions for common input values to save the user from typing.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该子类化
AutoCompleteTextView
并重写enoughToFilter()
以始终返回true
。之后,您可以调用performFiltering("",0)
(它是一个受保护的函数,因此您可以通过类中的公共函数导出此调用)。像这样的东西:
You should subclass
AutoCompleteTextView
and overrideenoughToFilter()
to returntrue
all the time. After that you can callperformFiltering("",0)
(it's a protected function, so you can export this call via a public function in your class).Something like that:
Itay Kahana的回答确实是正确的。我唯一要添加的是,不要创建 temp() 函数,而是重写 onFocusChanged 函数。我个人使用了以下内容:
Itay Kahana's answer is indeed correct. The only thing I would add is that instead of creating a temp() function, to override the onFocusChanged function. Personally I used the following:
如果您不需要它是动态的,我会在资源中包含一个字符串数组,然后在即将查看 AutoCompleteTextView 时加载该数组。喜欢:
可以在 http://developer.android.com/reference/ 上找到android/widget/AutoCompleteTextView.html
我已经做过几次的另一种允许它向用户学习的方法是使用 IE 的数据库连接和简单的光标。创建数据库时,您可以插入一些默认值。
这是使用简单光标适配器的示例: http ://androidcommunity.com/forums/f4/how-to-use-autocompletetextview-with-simplecursoradapter-15875/
编辑1:
在用户开始输入之前显示列表的一种想法是在 EditText 下面有一个简单的列表视图。不确定您是否可以调用 autocompletetextview 来显示建议,应该可以。也许您需要创建自己的 autocompletetextiew 类。
If you dont need it to be dynamic I would go by having a string array in the resources, and then just load the array when the AutoCompleteTextView is about to be viewed. Like:
Which can be found on http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
Another way which I have done a couple of times which allows it to learn from the user is o use a database connection with IE a simple cursor. When you create the db you could insert some default values.
Here´s an example with using simple cursor adapter: http://androidcommunity.com/forums/f4/how-to-use-autocompletetextview-with-simplecursoradapter-15875/
Edit 1:
One idea to show the list before the user starts type is to have a simple listview below the EditText. Not sure if you could call the autocompletetextview to show the suggestions, should be possible somehow. Perhaps you need to create your own autocompletetextiew class.