Activity_mian.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.autocompletetextviewdemo.MainActivity"
android:orientation="vertical">
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要搜索的关键字"
android:completionThreshold="2"/>
</LinearLayout>
MainActivity.java
package com.example.autocompletetextviewdemo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
public class MainActivity extends ActionBarActivity {
//声明控件
// AutoCompleteTextView actv;
MultiAutoCompleteTextView mactv;
//初始化数据源,去匹配文本框中输入的关键字
String[] str={"beijing","beijing1","beijing2","nanjing","yanjing","yantai","nanguo"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
// actv=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
mactv=(MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
//需要一个适配器
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, str);
//将适配器与控件绑定
// actv.setAdapter(adapter);
mactv.setAdapter(adapter);
//设置分隔符,以逗号为分隔符
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer() );
}
}