一、理论学习
public class
AutoCompleteTextView
extends EditText
implements Filter.FilterListener
java.lang.Object
↳
android.view.View
↳
android.widget.TextView
↳
android.widget.EditText
↳
android.widget.AutoCompleteTextView
Known Direct Subclasses
MultiAutoCompleteTextView
MultiAutoCompleteTextView
An editable text view, extending AutoCompleteTextView, that can show completion suggestions for the substring of the text where the user is typing instead of necessarily for the entire thing.
ClassOverview
Aneditable text view that shows completion suggestions automatically while theuser is typing. The list of suggestions is displayed in a drop down menu fromwhich the user can choose an item to replace the content of the edit box with.
Thedrop down can be dismissed at any time by pressing the back key or, if no itemis selected in the drop down, by pressing the enter/dpad center key.
Thelist of suggestions is obtained from a data adapter and appears only after agiven number of characters defined by the threshold.
public class
MultiAutoCompleteTextView
extends AutoCompleteTextView
java.lang.Object
↳
android.view.View
↳
android.widget.TextView
↳
android.widget.EditText
↳
android.widget.AutoCompleteTextView
↳
android.widget.MultiAutoCompleteTextView
ClassOverview
Aneditable text view, extending AutoCompleteTextView,that can show completion suggestions for the substring of the text where theuser is typing instead of necessarily for the entire thing.
Youmust must provide a MultiAutoCompleteTextView.Tokenizer to distinguish the various substrings.
二、实践
1、创建AutoCompleteTextViewActivity
packagecom.figo.helloworld;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.ArrayAdapter;
importandroid.widget.AutoCompleteTextView;
importandroid.widget.MultiAutoCompleteTextView;
/**
* @author zhuzhifei
* @版权所有
*/
publicclass AutoCompleteTextViewActivity extends Activity {
private static final String[] cities =new String[] { "北京","上海",
"广州", "深圳","香港", "台北", "重庆","南京",
"南昌", "南宁"};
private AutoCompleteTextViewautoCompleteTextView = null;
private MultiAutoCompleteTextViewmultiAutoCompleteTextView = null;
@Override
protected void onCreate(BundlesavedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.autocompletetextviewtest);
autoCompleteTextView =(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
multiAutoCompleteTextView =(MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
// 创建适配器
ArrayAdapter<String> adapter= new ArrayAdapter<String>(
this,android.R.layout.simple_dropdown_item_1line, cities);
autoCompleteTextView.setAdapter(adapter);
// 设置输入多少字符后提示,默认值为1
autoCompleteTextView.setThreshold(1);
multiAutoCompleteTextView.setAdapter(adapter);
multiAutoCompleteTextView.setThreshold(1);
// 用户必须提供一个MultiAutoCompleteTextView.Tokenizer用来区分不同的子串。
multiAutoCompleteTextView
.setTokenizer(newMultiAutoCompleteTextView.CommaTokenizer());
}
}
2、运行效果
AutoCompleteTextView:
MultiAutoCompleteTextView: