一、功能
可支持选择多个值(在多次输入的情况下),分别用分隔符分开,并且在每个值选中的时候再次输入值时会自动去匹配,可用在发送短信,发邮件时选择联系人这种类型中
二、独特属性
android:completionThreshold = "2" ——设置输入多少字符时自动匹配
三、设置分隔符
mtxt.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());——设置以逗号作为分隔符
四、代码演示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionThreshold="2" android:hint="请输入要搜索的关键词" > </MultiAutoCompleteTextView> </LinearLayout>
package com.muke.textview_edittext; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; import android.app.Activity; public class MainActivity extends Activity{ private MultiAutoCompleteTextView MautoTextVIew; //第三步:初始化数据源——匹配文本框中输入的内容 private String[] res = {"beijing1","beijing2","beijing3","shanghai1","shanghai2","shenzhen1","shenzhen2","guangzhou1","guangzhou2"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //第一步:找到需要操作的控件 MautoTextVIew = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1); //第二步:需要一个适配器 //android.R.layout.simple_list_item_1:是系统自带的布局 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, res); //第四步:将adapter与当前AutoCompleteTextView绑定 MautoTextVIew.setAdapter(adapter); //第五步:设置分隔符(这里是以逗号为分隔符) MautoTextVIew.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } }
时间: 2024-12-20 19:31:02