废话不说,先看效果:
实现如下: 先绑定两个adapter:
public class MainActivity extends Activity {
private AutoCompleteTextView autoCompleteTextView ;
private MultiAutoCompleteTextView multiAutoCompleteTextView ;
//定义字符串数组作为提示的文本
private String[] names = {"牛人—盖子", "牛人—斯蒂芬", "牛人—杂刻伯各","牛人—秃临"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建ArrayAdapter封装数组
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,names);
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.auto);
//设置adapter
autoCompleteTextView.setAdapter(adapter);
multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.mauto) ;
//设置Adapter
multiAutoCompleteTextView.setAdapter(adapter);
//为MultiAutoCompleteTextView设置分隔符
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
布局文件如下: 一个:AutoCompleteTextView(只能选择一次) 一个:MultiAutoCompleteTextView(可以进行多次选择)
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".MainActivity" >
<!--定义一个自动完成的文本框 -->
<!--制定输入一个字符后进行展示-->
<AutoCompleteTextView
android:id="@+id/auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionHint="请选择您喜欢的图书"
android:dropDownHorizontalOffset="10dp"
android:completionThreshold="1"/>
<!--定义一个MultiAutoCompleteTextView 组件-->
<MultiAutoCompleteTextView
android:id="@+id/mauto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"/>
</LinearLayout>