AutoCompleteTextView和MultiAutoCompleteTextView使用详解


控件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".UIDemoActivity" >

    <AutoCompleteTextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/autoCompleteTextView1"
        android:hint="@string/completeTextView1Hint"
        android:completionThreshold="2"
        >
	</AutoCompleteTextView>        
    <MultiAutoCompleteTextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/multiAutoCompleteTextView1"
        android:hint="@string/completeTextView1Hint"
        android:completionThreshold="1"
        >
	</MultiAutoCompleteTextView>        
</LinearLayout>

Activity代码:

package com.werw.UIDemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

/**
 * @author frank
 *
 */
public class UIDemoActivity extends Activity {
	private AutoCompleteTextView acTextView;
	private MultiAutoCompleteTextView macTextView;
	private String[] res = {"hhb","wz","gh","gmm","nsb","abc","ab2"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /*
         * 第一步初始化控件
         * 第二步需要一个适配器
         * 第三步初始化数据源,这个数据源去匹配输入的内容
         * 第四步将适配器和控件绑定
         */
        acTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
        macTextView = (MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,res);
        acTextView.setAdapter(adapter);
        macTextView.setAdapter(adapter);
        //设置不同值之间的分隔符号
        macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }
}


你可能感兴趣的:(android,安卓控件)