1.在XML配置控件(两种有MultiAutoCompleteTextView和AutoCompleteTextView)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <MultiAutoCompleteTextView android:id="@+id/tv_multiAuto" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:completionThreshold="1" /> <AutoCompleteTextView android:id="@+id/tv_auto" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:completionThreshold="1" /> </LinearLayout>
2.实例化两种控件
private MultiAutoCompleteTextView multiAutoComplete; private AutoCompleteTextView autoCompleteTextView;
this.multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.tv_multiAuto); this.autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.tv_auto);
3.准备数据源,在下拉列表显示的数据
String[] datas = new String[]{ "Hello", "Hay", "Ha", "World", "Word", "Wa!" };
4.设置适配器,为控件填充数据
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,datas); this.multiAutoComplete.setAdapter(adapter); this.autoCompleteTextView.setAdapter(adapter);
5.假如是多个补齐控件还可以设置间隔符
this.multiAutoComplete.setTokenizer( new MultiAutoCompleteTextView.CommaTokenizer());