1.acrtivity_main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true" >
<AutoCompleteTextView
android:id="@+id/actv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:completionThreshold="1"
android:dropDownHorizontalOffset="20dp"/>
</LinearLayout>
2.定义一个字符串数组:
String books[] = new String[] {"linjunjie","lilianjie","chenglong","wujing","wangbaoqiang","huojianhua","zhangjie","zhangyue","bigbang"};
<pre name="code" class="html"> setContentView(R.layout.activity_main);
AutoCompleteTextView auto = (AutoCompleteTextView)findViewById(R.id.actv_show);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,books);
auto.setAdapter(adapter);
为控件添加适配器。
布局文件中:
android:completionThreshold="1"
这个意思是1个字符进行匹配,默认是2个字符匹配。
很简单的一个控件。