一、

 1  public class CountriesActivity extends Activity {
 2      protected void onCreate(Bundle icicle) {
 3          super.onCreate(icicle);
 4          setContentView(R.layout.countries);
 5 
 6          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
 7                  android.R.layout.simple_dropdown_item_1line, COUNTRIES);
 8          AutoCompleteTextView textView = (AutoCompleteTextView)
 9                  findViewById(R.id.countries_list);
10          textView.setAdapter(adapter);
11      }
12 
13      private static final String[] COUNTRIES = new String[] {
14          "Belgium", "France", "Italy", "Germany", "Spain"
15      };
16  }

 

二、

1.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 3     android:orientation="horizontal"
 4     android:layout_width="fill_parent" 
 5     android:layout_height="wrap_content"
 6     android:padding="5dp">
 7     <TextView
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:text="Country" />
11     <AutoCompleteTextView android:id="@+id/autocomplete"
12         android:layout_width="fill_parent"
13         android:layout_height="wrap_content"
14         android:layout_marginLeft="5dp"/>
15 </LinearLayout>

2.list_item.xml自定提示样式

1 <?xml version="1.0" encoding="utf-8"?>
2 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
3     android:layout_width="fill_parent"
4     android:layout_height="fill_parent"
5     android:padding="10dp"
6     android:textSize="16sp"
7     android:textColor="#000">
8 </TextView>

3.java

 1 package mars.autocomplete;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.app.Activity;
 7 import android.os.Bundle;
 8 import android.widget.ArrayAdapter;
 9 import android.widget.AutoCompleteTextView;
10 
11 public class MainActivity extends Activity {
12     /** Called when the activity is first created. */
13     AutoCompleteTextView autoCompleteTextView = null;
14     static final String[] COUNTRIES = new String[] {
15           "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
16           "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
17           "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
18           "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
19           "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
20           "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
21           "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
22           "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
23           "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
24           "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
25           "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
26           "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
27           "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
28           "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
29           "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
30           "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
31           "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
32           "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
33           "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
34           "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
35           "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
36           "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
37           "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
38           "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
39           "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
40           "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
41           "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
42           "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
43           "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
44           "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
45           "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
46           "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
47           "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
48           "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
49           "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
50           "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
51           "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
52           "Ukraine", "United Arab Emirates", "United Kingdom",
53           "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
54           "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
55           "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
56         };
57     @Override
58     public void onCreate(Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60         setContentView(R.layout.main);
61         //通过ID得到AutoCompleteTextView对象
62         autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autocomplete);
63         //创建一个list,为ArrayAdapter提供数据
64         List<String> list = new ArrayList<String>();
65         list.add("测试测试");
66         list.add("测试test");
67         //创建一个ArrayAdapter对象
68         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,list);
69         //将ArrayAdapter设置给AutoCompleteTextView对象
70         autoCompleteTextView.setAdapter(arrayAdapter);
71         
72     }
73 }

 

You can do anything you set your mind to, man!