1.Activity如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 |
package
com.travelsky.autotest;import
java.util.ArrayList;import
java.util.HashMap;import
android.os.Bundle;import
android.app.Activity;import
android.view.Menu;import
android.view.View;import
android.widget.AdapterView;import
android.widget.AdapterView.OnItemClickListener;import
android.widget.AutoCompleteTextView;import
android.widget.SimpleAdapter;import
android.widget.TextView;public
class
AutoTestActivity extends
Activity { /** Called when the activity is first created. */ ArrayList<HashMap<String, String>> list = new
ArrayList<HashMap<String, String>>(); AutoCompleteTextView ac; @Override public
void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_auto_test); addItems(); ac = (AutoCompleteTextView) findViewById(R.id.autocomplete); SimpleAdapter notes = new
SimpleAdapter(this, list, R.layout.main_item_three_line_row, new
String[] { "brandSearchText", "brandName"
}, new
int[] { R.id.searchText, R.id.brandName }); ac.setAdapter(notes); ac.setThreshold(1); <br><br> //以下代码是为了格式化点击提示选项后最后显示的内容,否则会把Map的key和value都显示出来 ac.setOnItemClickListener(new
OnItemClickListener() { @Override public
void
onItemClick(AdapterView<?> arg0, View arg1, int
arg2, long
arg3) { TextView tv = (TextView)arg1.findViewById(R.id.brandName); ac.setText(tv.getText().toString()+" "); ac.setSelection((ac.getText().toString()).length()); } }); } private
void
addItems() { HashMap<String, String> item; item = new
HashMap<String, String>(); item.put("brandSearchText", "dongfanghangkonggongsi DFHKGS dfhkgs"); item.put("brandName", "东方航空公司"); list.add(item); item = new
HashMap<String, String>(); item.put("brandSearchText", "nanfanghangkonggongsi NFHKGS nfhkgs"); item.put("brandName", "南方航空公司"); list.add(item); item = new
HashMap<String, String>(); item.put("brandSearchText", "guojihangkonggongsi GJHKGS gjhkgs"); item.put("brandName", "国际航空公司"); list.add(item); item = new
HashMap<String, String>(); item.put("brandSearchText", "sichuanhangkonggongsi SCHKGS schkgs"); item.put("brandName", "四川航空公司"); list.add(item); }} |
其中,SimpleAdapter的参数如下:
第1个,this
第2个,list: 需要传入形如 List<? extends Map<String, ?>>类型的集合对象
第3个,该参数指定一个页面布局的ID
第4个,该参数应该是一个String[]类型的参数,该参数决定提取Map<String,?>对象中哪些key对应的value来生成列表项
第5个,该参数应该是一个int[]类型的参数,该参数决定填充哪些ID的组件
2.主界面布局文件如下
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="航空公司:"
android:textSize="20sp"/>
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入航空公司"
/>
</LinearLayout>
3.SimpleAdapter使用的自定义布局文件如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/brandName" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/searchText" android:visibility="gone" /> </LinearLayout>
其中,android:visibility="gone" 表示隐藏TextView,并且不保留该控件所占位置, 以便隐藏拼音及拼音缩写
安卓AutoCompleteTextView 支持输入中文或拼音或拼音缩写的实现方式
原文:http://www.cnblogs.com/xiaomimi/p/3516006.html