AutoCompleteTextView
1.功能:动态匹配输入的内容,如百度搜索引擎当输入文本时可以根据内容显示匹配的热门信息。2.独特属性:android:completionThreshold 设置输入多少字符时自动匹配使用AutoCOmpleteTextView实现自动匹配输入的内容
private AutoCompleteTextView acTextView;初始化控件acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);初始化数据源(代码里res数组又增加了一些)private String[] res = new String[] { "moon", "month", "moonlight", "moonlight poet" };创建一个适配器(这里使用ArrayAdapter)ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, res);讲adapter与当前AutoCompleteTextView绑定(通过.setAdapter()方法绑定)acTextView.setAdapter(adapter);在activity_main.xml设置当输入多少字符时出现adapter中的内容android:completionThreshold="1"MultiAutoCompleteTextView
1.功能可支持选择多个值(在多次输入的情况下),分别用分隔符分开,并且在每个值选中的时候再次输入值会自动去匹配,可用在可短信,发邮件时选择联系人这种类型当中。2.独特属性:android:completionThreshold 设置输入多少字符时自动匹配3.设置分隔符mtxt.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());使用MultiAutoCOmpleteTextView实现自动匹配输入的内容
具体步骤和AutoCompleteTextView类似。设置分隔符(设置以逗号为分隔符)macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());package com.example.autocompletetextviewandmulti;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.MultiAutoCompleteTextView;import android.os.Build;public class MainActivity extends ActionBarActivity { private AutoCompleteTextView acTextView; private MultiAutoCompleteTextView macTextView; private String[] res = new String[] { "apple", "banana", "China", "delicious", "eight", "fight", "good", "hello", "intelligence", "just", "kilometer", "like", "moon", "month", "moonlight", "moonlight poet", "night", "ok", "pear", "queue", "ring", "stack", "tail", "unique", "very", "week", "x-Men", "yellow", "zero" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); ArrayAdapteradapter = new ArrayAdapter (this, android.R.layout.simple_expandable_list_item_1, res); acTextView.setAdapter(adapter); macTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1); macTextView.setAdapter(adapter); macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); }}
效果: