您的位置:首页 > 其它

AutoCompleteTextView的用法

2012-05-14 09:37 267 查看
这次介绍AutoCompleteTextView的两种用法

第一种:使用字符串数组为内容来源

<AutoCompleteTextView
android:id="@+id/auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

/>


这是AutoCompleteTextView的布局文件。

AutoCompleteTextView tv=null;
tv=(AutoCompleteTextView) findViewById(R.id.auto);
String []str=new String[]{"Chine","Japan","Korean","Russian","USA","Hong Kong"};


下面是设置ArrayAdapter

ArrayAdapter aaa=new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, str);
tv.setAdapter(aaa);
这样就完成了AutoCompleteTextView。

效果图



第二种方法:使用XML为数据来源

下面是xml文件,在values/strings.xml中

<string-array name="str">
<item>China</item>
<item>Korean</item>
<item>Japan</item>
</string-array>
下面设置ArrayAdapter
ArrayAdapter aaa=ArrayAdapter.createFromResource(this, R.array.str, android.R.layout.simple_spinner_item);
aaa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
tv.setAdapter(aaa);
这样就完成了



从效果图来看,有些不一样,我们可以根据需要来选择。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: