1.AutoCompleteTextView:动态匹配输入的内容,如百度搜索引擎当输入文本时可以根据内容显示匹配的热门信息。
属性:completionThreshold=“2” //设置输入多少字符时自动匹配
autoComleteTextView.java
package com.example.xuhai.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
/**
* Created by xuhai on 2016/8/4.
*/
public class autComlpeteTextView extends Activity{
private AutoCompleteTextView actv;
private String[] array={"beijing1","beijing2","yuncheng1","yuncheng2","nancheng"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.auocompletetextview);
/*
*第一步:初始化控件
第二步:需要一个适配器
第三步:初始化数据源----这个数据源去匹配文本框输入的内容
第四步:将adpter与当前的AutoCompleteTextView绑定
*/
actv=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array);
actv.setAdapter(adapter);
}
}
autocompleteviewtext.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <AutoCompleteTextView android:completionThreshold="2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入你要查找的内容" android:id="@+id/autoCompleteTextView" android:layout_gravity="center_horizontal" /> </LinearLayout>运行结果
2.MultiAutoCompleteTextView:支持选择多个值(在多次输入的情况下),分别用分隔符隔开,并且在每个值选中的时候再次输入值时会自动去匹配,可用在发短信,发邮件的时候选择联系人这种类型中。
属性:completionThreshold=“2”;//设置输入多少字符时自动匹配
mtxt.setTokenizer(new MultiAutoCompleteTextView.commaTokenizer());//设置分隔符(这里是设置逗号分隔))