Android组件系列(1):自动完成输入内容的组件(AutoCompleteTextView )
2010-04-25 12:05
567 查看
AutoCompleteTextView 和 EditText 组件类似,都可以输入文本。但 AutoCompleteTextView 组件可以和一个字符串数组或 List 对象绑定,当用户输入两个及以上字符时,系统将在 AutoCompleteTextView 组 件下方列出字符串数组中所有以输入字符开头的字符串,这一点和 www.Google.com 的搜索框非常相似,当输入某一个要查找的字符串时, Google 搜索框就会列出以这个字符串开头 的最热门的搜索字符串列表。
AutoCompleteTextView 组件在 XML 布局文件中使用 <AutoCompleteTextView> 标签来表示,该标签的使用方法与 <EditText> 标签相同。如果要让 AutoCompleteTextView 组件显示辅助输入列表,需要使用 AutoCompleteTextView 类的 setAdapter 方法指定一个 Adapter 对象,代码如下:
String[] autoString = new String[]{ " a " , " ab " , " abc " , " bb " , " bcd " , " bcdf " , " 手 机 " , " 手机操作系统 " , " 手 机软件 " };
ArrayAdapter < String > adapter = new ArrayAdapter < String > ( this ,
android.R.layout.simple_dropdown_item_1line, autoString);
AutoCompleteTextView autoCompleteTextView =
(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
autoCompleteTextView.setAdapter(adapter);
运行上面代码后,在文本框中输入“手机”, 就会显示如图 1 所 示的效果。
除了 AutoCompleteTextView 组 件外,我们还可以使用 MultiAutoCompleteTextView 组件来完成连续输入的功能。也就是说,当输入完一个字符串后,在该字符串后面输入一个逗号( , ),在逗号前后可以有任意多个空格,然后 再输入一个字符串(例如,“手机”),仍然会显示辅助输入的列表,但要使用 MultiAutoCompleteTextView 类的 setTokenizer 方法指定 MultiAutoCompleteTextView.CommaTokenizer 类的对象实例(该对象表示输入多个字符串时的分隔符为逗号),代码如下:
MultiAutoCompleteTextView multiAutoCompleteTextView =
(MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView);
multiAutoCompleteTextView.setAdapter(adapter);
multiAutoCompleteTextView.setTokenizer( new MultiAutoCompleteTextView.CommaTokenizer());
运行上面的代码后,在屏幕的第 2 个文本框中输入“ ab , ” 后,再输入“手机”,会显示如图 2 所示的效果。
本文来自CSDN博客,转载于:http://blog.csdn.net/nokiaguy/archive/2010/04/21/5513978.aspx
相关文章推荐
- Android组件系列(1):自动完成输入内容的组件(AutoCompleteTextView )
- Android 组件系列(1):自动完成输入内容的组件(AutoCompleteTextView )
- [Android] AutoCompleteTextView:自动完成输入内容的控件
- 输入内容自动完成文本框(AutoCompleteTextView)
- 输入内容自动完成文本框(AutoCompleteTextView )
- 输入内容自动完成文本框(AutoCompleteTextView )
- [Android] AutoCompleteTextView:自动完成输入内容的控件(自动补全)
- AutoCompleteTextView自动完成输入内容
- 安卓控件——AutoCompleteTextView和MultiAutoCompleteTextView实现自动匹配输入的内容
- 使用AutoCompleteTextView实现自动匹配输入的内容
- Android之UI学习篇五:AutoCompleteTextView自动完成输入内容
- Android入门(4)——AutoComplete TextView实现动态自动匹配输入的内容
- Android之UI学习篇五:AutoCompleteTextView自动完成输入内容
- AutoCompleteTextView自动完成输入内容的控件
- AutoCompleteTextView,MultiAutoCompleteTextView自动完成输入内容的控件
- Android开发高级组件之自动完成文本框(AutoCompleteTextView)用法示例【附源码下载】
- 使用autoCompleteTextView以及MultiAutoCompleteTextView实现自动匹配输入内容
- Android组件复习之基本组件06—autocompletetextview自动完成文本框
- Android组件复习之基本组件06—autocompletetextview自动完成文本框
- 对自动完成文本框(AutoCompleteTextView)组件的一点补充