Android开发适配器系列:AutoCompleteTextView和MultiAutoCompleteTextView

1、AutoCompleteTextView介绍:
AutoCompleteTextView是EditText的子类,可以使用EditText所有的属性,例如字体,颜色等。此外,提供android:completionThreshold,用于表明最小要敲入多少字符才开始显示list filter。我们可以通过setAdapter设置一个adapter来给出list的内容,但是由于用户可以自行填入内同,因此不支持selectedlistener,我们可以注册一个TextWatcher,就如同其他EditTextwidget一样,来检测特系统的变化,它在用户从list中选择或者人工填入都可以监测到。
2、MultiAutoCompleteTextView介绍:
MultiAutoCompleteTextView是AutoCompleteTextView的子类,一个可编辑的文本视图,能够对用户键入的文本进行有效地扩充提示,而不需要用户输入整个内容。(用户输入一部分内容,剩下的部分系统就会给予提示)。
3、运行效果图:
Android开发适配器系列:AutoCompleteTextView和MultiAutoCompleteTextView_第1张图片Android开发适配器系列:AutoCompleteTextView和MultiAutoCompleteTextView_第2张图片Android开发适配器系列:AutoCompleteTextView和MultiAutoCompleteTextView_第3张图片
4、实例源码:
main.xml布局文件


    

    
    

    

    
    

Item.xml布局文件


MainActivity类
package com.andieguo.autocomplete;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;

/**
 * @author andieguo
 * 
 */
public class MainActivity extends Activity implements TextWatcher {
	private static final String[] autostr = new String[] { "a", "ab", "abc",
		"abcd", "b", "bc", "bcd" };
	private TextView selection = null;
	private TextView mulselection = null;
	private AutoCompleteTextView autoCompleteTextView = null;
	private MultiAutoCompleteTextView multiAutoCompleteTextView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		selection = (TextView) findViewById(R.id.selection);
		mulselection = (TextView) findViewById(R.id.mulselection);
//		ArrayAdapter adapter = new ArrayAdapter(this,
//				android.R.layout.simple_dropdown_item_1line, autostr);//使用系统自带的item
		ArrayAdapter adapter = new ArrayAdapter(this,
				R.layout.item, autostr);//使用自定义item
		
		autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
		autoCompleteTextView.setAdapter(adapter);
		autoCompleteTextView.setThreshold(1); //更改触发提示的字符长度,default=2,也可以在xml文件里进行配置
		//可连续输入关键字(abc,ab,a,abcd)
		multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView);
		multiAutoCompleteTextView.setAdapter(adapter);
		multiAutoCompleteTextView
				.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
		//文本改变时触发该事件
		autoCompleteTextView.addTextChangedListener(this);//设置监听很关键
		multiAutoCompleteTextView.addTextChangedListener(this);
		autoCompleteTextView.setOnFocusChangeListener(new OnFocusChangeListener() {// 设置焦点改变事件
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				AutoCompleteTextView view = (AutoCompleteTextView) v;
				if (hasFocus) {
					view.showDropDown();// 让下拉框弹出来
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		selection.setText("用户输入:"+autoCompleteTextView.getText());
		mulselection.setText("用户输入:"+multiAutoCompleteTextView.getText());
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
	}

}

你可能感兴趣的:(Android)