自动完成文本框(AutoCompleteTextView与MultiAutoCompleteTextView)关联适配器

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context="com.example.lesson6_5_id19.MainActivity" >
 7     <!--单个字段扩充提示 
 8     completionThreshold是控制用户输入第几个字符开始提示,最小值是1-->
 9     <AutoCompleteTextView 
10         android:id="@+id/actv"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:hint="1.请输入联系人"
14         android:completionThreshold="1"
15         android:singleLine="true"/>
16     
17     <!-- 多个字段扩充提示 -->
18     <MultiAutoCompleteTextView 
19         android:id="@+id/mactv"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:hint="2.请输入联系人"
23         android:completionThreshold="1"/>
24 
25 </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>
 1 package com.example.lesson6_5_id19;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.widget.ArrayAdapter;
 6 import android.widget.AutoCompleteTextView;
 7 import android.widget.MultiAutoCompleteTextView;
 8 
 9 public class MainActivity extends Activity {
10     // 把提示的内容放到一个String数组里面
11     String[] names = {"zhangshan","lisi","wangwu","zhaoliu",
12             "zhangshan","lisi","wangwu","zhaoliu",
13             "zhangshan","lisi","wangwu","zhaoliu",
14             "zhangshan","lisi","wangwu","zhaoliu",
15             "zhangshan","lisi","wangwu","zhaoliu",
16             "zhangshan","lisi","wangwu","zhaoliu",};
17     AutoCompleteTextView actv;
18     MultiAutoCompleteTextView mactv;
19     ArrayAdapter<String> adapter;
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         actv = (AutoCompleteTextView) findViewById(R.id.actv);
25         mactv = (MultiAutoCompleteTextView) findViewById(R.id.mactv);
26         // 适配器
27         // 将数据与视图关联在一起
28         // 系统已经帮我们写好了很多layout
29         // adapter = new ArrayAdapter<String>(this//1.上下文
30         // ,R.layout.item_actv_layout//2.布局
31         // ,R.id.tv//该布局上的TextView的id号
32         // ,names);//最后一个是数据
33         // 选择系统的layout,里面的TextView的id号可以忽略不写
34         adapter = new ArrayAdapter<String>(this, R.layout.activity_item, R.id.tv, names);
35         //使用“,”来分割。
36         mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
37         // 将适配器设置到view上
38         actv.setAdapter(adapter);
39         //设置适配器之前,必须要知道分隔符是什么样的。
40         mactv.setAdapter(adapter);
41     }
42 }
原文地址:https://www.cnblogs.com/lxjhoney/p/6485486.html