MultiAutoCompleteTextView

MultiAntoCompleteTextView在MainActivity中的实现
1.初始化控件
2.设置适配器
3.初始化数据源
4.将MultiAutoCompleteTextView控件和适配器联系起来
5.设置控件的分隔符

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.autocomplet.MainActivity" >

    <AutoCompleteTextView
        android:completionThreshold="3"
        android:hint="请输入您要搜索的内容"
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="17dp"
        android:ems="10"
         >

        <requestFocus />
    AutoCompleteTextView>

    <MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/autoCompleteTextView1"
        android:layout_marginTop="21dp"
        android:ems="10"
        android:hint="请输入收件人" />

RelativeLayout>

package com.example.autocomplet;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

public class MainActivity extends Activity {

    // 定义相关控件
    private AutoCompleteTextView acTextView;
    private String[] res = { "beijing1", "beijing2", "shanghai1", "shanghai2" };

    private MultiAutoCompleteTextView matcTextview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
         * 1.初始化控件 2.需要一个适配器 3.初始化数据--这数据源去匹配文本输入的内容
         * 4.将adpter与当前AutoCompleteText绑定
         */
        acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, res);
        acTextView.setAdapter(adapter);
        // MultiAutoCompleteTextView
        matcTextview = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
        matcTextview.setAdapter(adapter);
        // 设置以逗号为分隔符为结束符号
        matcTextview
                .setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }

}

你可能感兴趣的:(android)