直接给代码,先在xml中定义一个控件:
<AutoCompleteTextView android:id="@+id/auto" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" />在Activity中定义并获取该控件:
private AutoCompleteTextView auto;设置适配器:
auto = (AutoCompleteTextView)this.findViewById(R.id.auto); List <String> list = new ArrayList<String>(); list.add("blue"); list.add("bluer"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.auto_item, list); auto.setAdapter(adapter);适配器中的布局auto_item.xml简单的一个TextView:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/auto_item" android:layout_width="fill_parent" android:layout_height="wrap_content" />
效果图: