Android中AutoCompleteTextView控件的使用

来源:互联网 发布:朱苏力 知乎 编辑:程序博客网 时间:2024/06/11 08:59

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


3.在OnCreate方法中:

<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个字符匹配。
很简单的一个控件。

0 0