<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="com.example.lesson6_5_id19.MainActivity" >
<!--单个字段扩充提示
completionThreshold是控制用户输入第几个字符开始提示,最小值是1-->
<AutoCompleteTextView
android:id="@+id/actv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="1.请输入联系人"
android:completionThreshold="1"
android:singleLine="true"/>
<!-- 多个字段扩充提示 -->
<MultiAutoCompleteTextView
android:id="@+id/mactv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="2.请输入联系人"
android:completionThreshold="1"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 自定义提示筐的TextView -->
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示文本"
android:gravity="center"
android:padding="10dp"
android:background="#ccffcc"
android:textColor="#ff0000"/>
</LinearLayout>
package com.example.lesson6_5_id19;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
public class MainActivity extends Activity {
// 把提示的内容放到一个String数组里面
String[] names = {"zhangshan","lisi","wangwu","zhaoliu",
"zhangshan","lisi","wangwu","zhaoliu",
"zhangshan","lisi","wangwu","zhaoliu",
"zhangshan","lisi","wangwu","zhaoliu",
"zhangshan","lisi","wangwu","zhaoliu",
"zhangshan","lisi","wangwu","zhaoliu",};
AutoCompleteTextView actv;
MultiAutoCompleteTextView mactv;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actv = (AutoCompleteTextView) findViewById(R.id.actv);
mactv = (MultiAutoCompleteTextView) findViewById(R.id.mactv);
// 适配器
// 将数据与视图关联在一起
// 系统已经帮我们写好了很多layout
// adapter = new ArrayAdapter<String>(this//1.上下文
// ,R.layout.item_actv_layout//2.布局
// ,R.id.tv//该布局上的TextView的id号
// ,names);//最后一个是数据
// 选择系统的layout,里面的TextView的id号可以忽略不写
adapter = new ArrayAdapter<String>(this, R.layout.activity_item, R.id.tv, names);
//使用“,”来分割。
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
// 将适配器设置到view上
actv.setAdapter(adapter);
//设置适配器之前,必须要知道分隔符是什么样的。
mactv.setAdapter(adapter);
}
}