[置顶] Android开发适配器系列:AutoCompleteTextView和MultiAutoCompleteTextView

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

<TextView
android:id="@+id/selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入:" />

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:hint="请输入文字" />
<!-- 在我们测试中,缺省为2,即我们敲入第2个字符的时候可以触发list显示 -->

<TextView
android:id="@+id/mulselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入:" />

<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:hint="请输入文字" />
<!-- 在我们测试中,缺省为2,即我们敲入第2个字符的时候可以触发list显示 -->

</LinearLayout>
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:textSize="16sp"
android:padding="10dp"
android:textColor="#0000aa">
</TextView>
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<String> adapter = new ArrayAdapter<String>(this,
//android.R.layout.simple_dropdown_item_1line, autostr);//使用系统自带的item
ArrayAdapter<String> adapter = new ArrayAdapter<String>(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
}

}