自动完成可编辑文本AutoCompleteTextView的使用

我们在百度或者Google中搜索信息所用的输入框,都是可以在我们输入少量文字的时候列出下拉菜单显示相关的搜索关键字,我们可以选择想要搜索的关键字而快速获取需要的信息。此功能即是使用了自动完成的可编辑文本输入框控件。在Android的UI开发中也有这样一个控件,它的名字叫AutoCompleteTextView,通过它我们可以实现类似搜索框那样的UI功能。
以下ATAAW.COM罗列下Android中的AutoCompleteTextView的具体使用方法。

A、在布局文件中的相应位置声明自动完成可编辑空间
<AutoCompleteTextView
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

B、在程序中加载Android布局
setContentView(R.layout.autocompletetextview);

C、构造数据源,一般采用数组或者通过数据库获取数据源
private String[] ary = new String[] {
"ATAAW.COM",
"随时随地",
"即兴时代",
"Android",
"Google",
};

D、通过数据源为空间创建适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line, //这里使用的是Android自带的Style
ary);

E、为控件制定适配器
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.editText);
textView.setAdapter(adapter);

至此,一个自动完成的AutoCompleteTextView可编辑文本框完成了。

转自:http://www.ataaw.com/develop/242.html

posted on 2012-08-10 21:51  封起De日子  阅读(126)  评论(0编辑  收藏  举报

导航