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());

<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"
    >

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:completionThreshold="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入你要搜索的关键词"
        >

        <requestFocus />
    </AutoCompleteTextView>

    <MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextView1"
        android:completionThreshold="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="请输入关键词(可多个,逗号分隔)" 
        />

    
</LinearLayout>
activity_main.xml

相关文章: