AutoCompleteTextView是用於總動完成輸入文本的組件,用好了是非常方便用戶的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionHint="選中班級"
android:completionThreshold="1" //定義了從第幾個字符開始顯示候補列表
android:dropDownHeight="300dp"/>
</LinearLayout>
對應的Java代碼
AutoCompleteTextView auto = (AutoCompleteTextView) findViewById(R.id.auto);
//使用AutoCompleteTextView要用到適配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_list_item_1,
new String[] { "android", "java", ".net" });//自動提示的選項
auto.setAdapter(adapter);