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 属性的意思是从第几个字符开始自动匹配,我设置成了从第二个字符开始自动匹配。![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)