您的位置:首页 > 移动开发 > Android开发

Android实现搜索功能 AutoCompleteTextView和MultiAutoCompleteTextView的用法

2016-08-07 12:25 375 查看
AutoCompleteTextView和MultiAutoCompleteTextView的用法:AutoCompleteTextView一般用在搜索框的使用,MultiAutoCompleteTextView一般使用在短信和邮件中。具体实现通过一个例子说明问题:
private AutoCompleteTextView acCompleteTextView;
private MultiAutoCompleteTextView mactv;
private String[] str = {"beijing","beijing tiananmen", "shanghai", "shanghai dongfangmingzhu", "lanzhou", "lanzhouzhongshanqiao", "lanzhou wuquanshan"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acCompleteTextView = (AutoCompleteTextView) this.findViewById(R.id.acTextView);
mactv = (MultiAutoCompleteTextView) this.findViewById(R.id.mactv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,str);
acCompleteTextView.setAdapter(adapter);

mactv.setAdapter(adapter);
/* 设置逗号为分隔符 */
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
布局文件中的内容为:
<AutoCompleteTextView
android:id="@+id/acTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="2"
android:hint="@string/content" />

<MultiAutoCompleteTextView
android:id="@+id/mactv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/content"
android:completionThreshold="2"/>
completionThreshold 属性的意思是从第几个字符开始自动匹配,我设置成了从第二个字符开始自动匹配。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐