这次介绍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>下面设置ArrayAdapterArrayAdapter 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);这样就完成了

从效果图来看,有些不一样,我们可以根据需要来选择。
本文介绍了Android中AutoCompleteTextView的两种实现方式:一种是以字符串数组作为数据源;另一种是通过XML文件定义数据源。这两种方式均使用了ArrayAdapter进行数据绑定,并展示了具体的实现步骤及效果对比。
22万+

被折叠的 条评论
为什么被折叠?



