AutoCompleteTextView
在Layout文件中添加AutoCompleteTextView控件后,在xml文件中添加属性android:completionThreshold,表示在输入多少个字符以后进行字符串的匹配,如下。
Layout
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_text1"
android:completionThreshold="3">
</AutoCompleteextView>
然后在Activity文件中,进行适配器和数据源的绑定和配置,如下。
Activity
private AutoCompleteTextView acTextView;
private String[] res = {"beijing1","beijing2","beijinng","shanghai"};
acTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
/*
* 1.创建适配器,并绑定数据源匹配文本框输入的内容
* 2.将适配器与控件绑定
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, res);
acTextView.setAdater(adapter);
MultiAutoCompleteTextView
MultiAutoCompleteTextView和AutoCompleteTextView类似,不同的是在Activity文件中多了一条设计间隔符的语句。
Layout
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_text2" >
</MultiAutoCompleteTextView>
Activity
private MultiAutoCompleteTextView muTextView;
private String[] res = {"beijing1","beijing2","beijinng","shanghai"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, res);
muTextView.setAdapter(adapter);
/*
*设置分隔符
*/
muTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());