Android学习(三) 自动完成的使用

1、AutoCompleteTextView

  自动完成功能,在文本框中输入字符,会出现匹配的自动提示。类似百度搜索。

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="城市:" />

    <AutoCompleteTextView
        android:id="@+id/actv1"
        android:completionThreshold="3"    //输入多少个字符会出现提示
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:hint="请输入城市" >
    </AutoCompleteTextView>
    
</LinearLayout >

Java代码

public class AutoCompleteTextViewDemo extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autocomplete_demo);

        //1、获取页面上的自动完成控件
        AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv1);
        //2、创建一个数组,保存的数据,字符串数组或者List<String>
        String[] city = new String[] { "beijing1", "shenzhen1", "shenzhen2", "beijing2", "shanghai" };
        //3、创建一个适配器对象,用来绑定数据到AutoCompleteTextView中的第一个为当前Activity对象,第二个参数为显示的样式,第三个为数据源.
        ArrayAdapter<String> citydatapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, city);
        //4、将适配器绑定到AutoCompleteTextView中
        actv.setAdapter(citydatapter);
    }
}

2、MultiAutoCompleteTextView

  支持多选的自动完成,可以实现类似邮箱收件人的多选效果。

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:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_marginTop="20dp">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="城市:"
            android:textSize="20sp" />

        <MultiAutoCompleteTextView
            android:id="@+id/mactv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:completionThreshold="2"
            android:ems="10"
            android:hint="请输入城市" />
    </LinearLayout>

</LinearLayout>

Java代码:

public class AutoCompleteTextViewDemo extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autocomplete_demo);

        //1、获取页面上的自动完成控件
        MultiAutoCompleteTextView mactv = (MultiAutoCompleteTextView) findViewById(R.id.mactv1);
        //2、创建一个数组,保存的数据,字符串数组或者List<String>
        String[] city = new String[] { "beijing1", "shenzhen1", "shenzhen2", "beijing2", "shanghai" };
        //3、创建一个适配器对象,用来绑定数据到AutoCompleteTextView中的第一个为当前Activity对象,第二个参数为显示的样式,第三个为数据源.
        ArrayAdapter<String> citydatapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, city);
        //4、将适配器绑定到AutoCompleteTextView中
        mactv.setAdapter(citydatapter);        
        //5、设置分隔符,以,进行分割
        mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }
}
原文地址:https://www.cnblogs.com/zhengcheng/p/4351343.html