創建一個能自動補全提示的文本輸入控件(widget),可以使用AutoCompleteTextView控件。提示從與控件關聯的ArrayAdapter提供的字符串集合得到。
在這個教程中,你將要創建一個自動提示國家名字的AutoCompleteTextView。
啟動一個新項目並命名為HelloAutoComplete。
創建一個名為list_item.xml的XML文件並把它保存在res/layout/文件夾下。編輯文件像下面這樣:
[html]
- <?xml version="1.0" encoding="utf-8"?>
- <TextView xmlns:Android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="10dp"
- android:textSize="16sp"
- android:textColor="#000">
- </TextView>
這個文件定義了一個簡單的TextView來顯示提示列表的每一項。
打開 res/layout/main.xml文件加入如下內容:
[html]
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:padding="5dp">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Country" />
- <AutoCompleteTextView android:id="@+id/autocomplete_country"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"/>
- </LinearLayout>
這個TextView是一個介紹AutoCompleteTextView控件的標簽。
打開HelloAutoComplete.java並在onCreate()函數中添加如下代碼:
[java]
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES);
- textView.setAdapter(adapter);
- }
把main.xml布局設置為內容視圖後,AutoCompleteTextView控件就會在布局中用findViewById(int)找到。接下來,一個新的ArrayAdapter初始化並把
COUNTRIES字符串數組(在下一步定義)的每個列表項綁定到 list_item.xml布局。最後,調用setAdapter()將AutoCompleteTextView和ArrayAdapter關聯起來,這樣字符串數組就能填充到提示列表中。
在HelloAutoComplete類中加入字符串數組:
[java]
- static final String[] COUNTRIES = new String[] {
- "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
- "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
- "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
- "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
- "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
- "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
- "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
- "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
- "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
- "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
- "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
- "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
- "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
- "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
- "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
- "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
- "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
- "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
- "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
- "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
- "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
- "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
- "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
- "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
- "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
- "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
- "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
- "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
- "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
- "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
- "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
- "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
- "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
- "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
- "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
- "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
- "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
- "Ukraine", "United Arab Emirates", "United Kingdom",
- "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
- "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
- "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
- };
這就是當用戶在AutoCompleteTextView控件輸入時提供給下拉列表的提示。
啟動這個應用程序。
當你輸入時,你將會看到如下:
更多信息
需要注意的是設計實踐中不建議硬編碼(hard-coded)字符串數組,因為你的應用程序將關注於行為,而不是內容。應用程序的內容如字符串會寫在代碼外部這樣做修改時就更方便和容易。這個教程中硬編碼的字符串只是做個例子重點放在了AutoCompleteTextView控件上。相反的,你的應用程序可以聲明這樣的字符串數組在XML文件中。這可以在你的項目中res/values/strings.xml 文件裡用<string-array>資源來完成。請看下面示例:
[html]
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="countries_array">
- <item>Bahrain</item>
- <item>Bangladesh</item>
- <item>Barbados</item>
- <item>Belarus</item>
- <item>Belgium</item>
- <item>Belize</item>
- <item>Benin</item>
- </string-array>
- </resources>
在ArrayAdapter使用這個資源字符串,只需要替換原來的ArrayAdapter構造部分,像下面一樣:
[java]
- String[] countries = getResources().getStringArray(R.array.countries_array);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, countries);