一、功能
动态匹配输入的内容,如百度搜索引擎当输入文本时,可以根据内容显示匹配的热门信息
二、独特属性
android:completionThreshold = "2" ——设置输入多少字符时自动匹配
三、代码演示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <AutoCompleteTextView android:id="@+id/autoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionThreshold="2" android:hint="请输入要搜索的关键词" /> </LinearLayout>
package com.muke.textview_edittext; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.app.Activity; public class MainActivity extends Activity{ private AutoCompleteTextView autoTextVIew; //第三步:初始化数据源——匹配文本框中输入的内容 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); //第一步:找到需要操作的控件 autoTextVIew = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); //第二步:需要一个适配器 //android.R.layout.simple_list_item_1:是系统自带的布局 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, res); //第四步:将adapter与当前AutoCompleteTextView绑定 autoTextVIew.setAdapter(adapter); } }