自定义 AutoCompleteTextView 行为 [英] Custom AutoCompleteTextView behavior

查看:27
本文介绍了自定义 AutoCompleteTextView 行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开箱即用,AutoCompleteTextView 小部件似乎无法匹配列表值中间的输入字符串——匹配总是在开头;例如,输入ar"匹配argentina",但不匹配hungary".

Out of the box, the AutoCompleteTextView widget does not seem to be able to match the input string in the middle of a list value - the matches are always made at the beginning; e.g., entering "ar" matches "argentina", but not "hungary".

如何搜索单词中间的文本?谁能给我一个主意?

How can I search for the text in the middle of the word ? Can anyone give me an idea ?

提前致谢!

推荐答案

你需要编写一个自定义的 Filter 类并实现 performFiltering 方法.此方法采用 CharSequence 参数,您可以使用它来执行您需要的任何字符串操作,以便从您的数据集生成匹配列表(在您的情况下,您可以使用 String.contains 而不是 String.startsWith).performFiltering 函数不在 UI 线程上运行.

You would need to write a custom Filter class and implement the performFiltering method yourself. This method takes a CharSequence argument, which you can use to perform whatever String operations you need in order to generate a list of matches from your dataset (in your case, you could use String.contains instead of String.startsWith). The performFiltering function is not run on the UI thread.

然后您将匹配列表作为 FilterResults 对象,它包含一个 Object values(你的匹配列表,可能是一个 ArrayList)和一个 int count 这是匹配列表的大小.

You then return your list of matches as a FilterResults object, which contains an Object values (your list of matches, probably an ArrayList) and an int count which is the size of your list of matches.

最后,实现publishResults 回调方法,它在工作线程生成匹配列表后返回,允许您在 AutoCompleteTextView 上调用 notifyDataSetChanged适配器,以便它可以显示结果.

Finally, implement the publishResults callback method, which returns once the worker thread has generated the list of matches, allowing you to call notifyDataSetChanged on your AutoCompleteTextView's adapter so that it can display the results.

这篇关于自定义 AutoCompleteTextView 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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