使用AutoCompleteTextView控件实现自动匹配输入内容
一、AutoCompleteTextView的特有属性
android:completionThreshold=”x “——设置输入第x个字符时开始匹配
二、预期效果
三、实现步骤
1.初始化控件
//Java
private AutoCompleteTextView acTextView;
acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
//XML
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:completionThreshold="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要搜索的内容" />
2.创建一个适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,res);
3.初始化数据源—用这些数据源去匹配输入的内容
private String[] res = {"Android","add","apple","AcFun","assistant","Beijing","ChenjiPayne","110"};
4.将适配器(adapter)与AutoCompleteTextView控件绑定
acTextView.setAdapter(adapter);
总体代码