2011-04-25 11:00:04
AutoCompleteTextView 是Android中输入提示框 虽然现在的输入法越来越强大,但还是让用户输入越方便越好
下面来看看代码
1.在main.xml中创建一个 AutoCompleteTextView 布局
<?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:completionThreshold="1" android:layout_marginLeft="5dp"/> <!-- android默认的是至少输入2个字符才显示提示 , android:completionThreshold 用来可以修改默认值 --> </LinearLayout>
2.为 AutoCompleteTextView 中的数据提供布局res/item_list.xml
<?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>
3.AutoCompleteText.java
package com.yin; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class AutoCompleteText extends Activity { private static String[] COUNTRIES = {"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"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autocomplete_country); /** * args[0] 不用解释了 * args[1] 为AutoCompleteTextView 中的每个数据提供布局 * args[3] 提供数据来源 */ ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list ,COUNTRIES); //启用监听器 actv.setAdapter(adapter); } }
在 ArrayAdapter中为AutoCompleteTextView提供数据源的另一种方法是在string.xml中添加string-array 如
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, AutoCompleteText!</string> <string name="app_name">AutoCompleteTextView</string> <string-array name="country"> <item>Angola</item> <item>Bahrain</item> <item>Bangladesh</item> <item>Botswana</item> <item>Colombia</item> </string-array> </resources>
获取数据
String[] countries = getResources.getStringArray(R.array.countr);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list ,country );