AutoComplete TextView可以達到簡易完成text的功能,常用在輔助搜尋,例如:字典程式或是搜尋引擎。
小試身手小範例:
本範例宣告一個Taiwan字串陣列,而內容為各縣市的英文名稱,在搜尋時如果輸入Ta時就會自動搜尋到在Taiwan這個陣列中符合Ta開頭的字串。
TextExam01.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
publicclass TestExam001 extends Activity {
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, Taiwan);
AutoCompleteTextView TaiwanCity = (AutoCompleteTextView)findViewById(R.id.city);
TaiwanCity.setAdapter(adapter);
}
privatestaticfinal String[] Taiwan = new String[]{
"keelung", "Taipei", "Taoyuan", "Hsinchu", "Hualien", "Taitung", "Yilan", "Pingtung", "Kaohsiung", "Tainan", "Chiayi", "Yunlin", "Nantou", "Taichung", "Changhua", "Miaoli"
};
}
main.xml
<?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:text="where are your from?"
android:textSize="20sp"
/>
<AutoCompleteTextView
android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
留言列表