android 高级组件之 AutoCompleteTextView

来源:互联网 发布:房地产的软件 编辑:程序博客网 时间:2024/05/16 11:42

自动完成文本框(AutoCompleteTextView) 从EditText 派生来的。实际上就是一个文本编辑框,

但他比文本框多了一个功能,就是当用用户输入一定定符时,自动完成文本框会出现一个下

接菜单,供用户选择,当用户选择后AutoCompleteTextView按用户的选择自动填充该文本框。

以下见demo,

第一步,我们需要事先准备好需要显示的数据,定义在strings.xml中

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">HelloAutoCompleteTextView</string>           <string-array name="province"><item>shan dong</item><item>jiang su</item><item>an hui</item><item>zhe jiang</item><item>fu jian</item><item>shang hai</item><item>guang dong</item><item>guang xi</item><item>hai nan</item><item>hu bei</item><item>hu nan</item><item>he nan</item><item>jiang xi</item><item>bei jing</item><item>tian jin</item><item>he bei</item><item>shan xi</item><item>nei meng gu</item><item>ning xia</item><item>xin jiang</item><item>qing hai</item><item>shan xi</item><item>gan su</item><item>si chuan</item><item>yun nan</item><item>gui zhou</item><item>xi zang</item><item>chong qing</item><item>liao ning</item><item>ji lin</item><item>hei long jiang</item><item>tai wan</item><item>xiang guang</item><item>ao men </item><item>山东</item><item>江苏</item><item>安徽</item><item>浙江</item><item>福建</item><item>上海</item><item>广东</item><item>广西</item><item>海南</item><item>湖北</item><item>湖南</item><item>河南</item><item>江西</item><item>北京</item><item>天津</item><item>河北</item><item>山西</item><item>内蒙古</item><item>宁夏</item><item>新疆</item><item>青海</item><item>陕西</item><item>甘肃</item><item>四川</item><item>云南</item><item>贵州</item><item>西藏</item><item>重庆</item><item>辽宁</item><item>吉林</item><item>黑龙江</item><item>台湾</item><item>香港</item><item>澳门</item></string-array></resources>

第二步,定义我们需要显示的布局,main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="省份:"    />    <AutoCompleteTextView android:hint="请输入省份"android:id="@+id/AutoCompleteTextView01" android:completionThreshold="1"android:layout_width="fill_parent" android:layout_height="wrap_content"></AutoCompleteTextView></LinearLayout>
第三步,我们需要定义一个下拉的布局,供别人选择提示 list_item.xml。

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:padding="10dp"    android:textSize="16sp"    android:textColor="#000"></TextView>

第四步,编码实现。

package android.basic.lesson10;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;public class MainHelloAutoCompleteTextView extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //定义数组        String[] province = getResources().getStringArray(R.array.province);                //定义数组适配器        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, province);                //找到自动完成组件        AutoCompleteTextView atv = (AutoCompleteTextView) findViewById(R.id.AutoCompleteTextView01);               //为其设置适配器        atv.setAdapter(adapter);    }}

效果图: