1.AutoCompleteTextView
(1)功能:动态匹配输入的内容,如百度搜索引擎当输入文本时可以根据内容
显示匹配的热门信息;
(2)独特属性:
android:completion Threshold="2"--设置输入多少字符时开始自动匹配
2.MultiAutoCompelteTextView
(1)功能:可支持选择多个值(在多次输入的情况下),分别用分隔符分开,
并且在每个值选中的时候再次输入值时会自动去匹配,可用在发
短信,发邮件时选择联系人这种类型当中;
(2)独特属性:
android:completion Threshold="2"--设置输入多少字符时开始自动匹配
(3)设置分隔符
mtxt.setTokenizer(new MultiAutoCompelteTextView.CommaTokenizer())
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.administrator.demo1.MainActivity">
<AutoCompleteTextView
android:completionThreshold="3"
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您要搜索的关键词"/>
<MultiAutoCompleteTextView
android:layout_below="@id/autoCompleteTextView1"
android:id="@+id/multiAutoCompelteTextView1"
android:hint="请输入您的收件"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
mainactivity:
package com.example.administrator.demo1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView.Tokenizer;
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView acTextView;
private String[] res={"beijing1","beijing2","beijing3","shanghai1","shanghai2"};
private MultiAutoCompleteTextView macTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
1.初始化控件
2.需要一个适配器
3.初始化数据源--这数据源去匹配文本框去输入的内容
4.将adpter与当前AutoCompleteTextView绑定
*/
acTextView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,res);
acTextView.setAdapter(adapter);
/*
1.初始化控件
2.需要一个适配器
3.初始化数据源--这数据源去匹配文本框去输入的内容
4.将adpter与当前MultiAutoCompleteTextView绑定
5.设置分隔符
*/
macTextView=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompelteTextView1);
macTextView.setAdapter(adapter);
//以逗号为分隔符
macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}