MainActivity.java
package com.ct7_AutocompletionTextView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity {
private AutoCompleteTextView textView;
private ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (AutoCompleteTextView) findViewById(R.id.edit);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, MyData.getData());
textView.setAdapter(adapter);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="填入下列文本框自动补全" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择国籍:" />
<AutoCompleteTextView
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选中"/>
</LinearLayout>
我的数据
MyData.java
package com.ct7_AutocompletionTextView;
public class MyData {
public static String[] getData() {
String[] countries = new String[] { "Afghanistan", "Albania",
"Algeria", "AmericanSamoa", "Andorra", "Angola",
"AntiguaandBarbuda", "Argentina", "Armenia", "Australia",
"Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh",
"Barbados", "Belarus", "Belau", "Belgium", "Belize", "Benin",
"Bhutan", "Bolivia", "BosniaandHerzegovina", "Botswana",
"Brazil", "Brunei", "Bulgaria", "Burkina-Faso", "Burundi",
"Cambodia", "Cameroon", "Canada", "CapeVerde",
"CentralAfricanRepublic", "Chad", "China", "Chile", "Colombia",
"Comoros", "Congo(Republicof)", "Congo(DemocraticRepublicof)",
"CostaRica", "Coted'Ivoire", "Croatia", "Cuba", "Cyprus",
"CzechRepublic", "Denmark", "Djibouti", "Dominica",
"DominicanRepublic", "EastTimor", "Ecuador", "Egypt",
"ElSalvador", "England", "EquatorialGuinea", "Eritrea",
"Estonia", "Ethiopia", "Fiji", "Finland", "France", "Gabon",
"Gambia", "Georgia", "Germany", "Ghana", "Greece", "Greenland",
"Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana",
"Haiti", "Honduras", "Hungary", "Iceland", "India",
"Indonesia", "Iran", "Iraq", "Israel", "Italy", "Jamaica",
"Japan", "Jordan", "Kazakhstan", "Kenya", "Kirghizia",
"Kiribati", "Kuwait", "Laos", "Latvia", "Lebanon", "Lesotho",
"Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
"Macedonia", "Madagascar", "Malawi", "Malaysia", "Mali",
"Malta", "MarshallIslands", "Mauritania", "Mauritius",
"Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia",
"Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru",
"Nepal", "Netherlands", "NewZealand", "Nicaragua", "Niger",
"Nigeria", "NorthernIreland", "NorthKorea", "Norway", "Oman",
"Pakistan", "Panama", "PapuaNewGuinea", "Paraguay",
"People'sRepublicofChina", "Peru", "Philippines", "Poland",
"Portugal", "PuertoRico", "Qatar", "RepublicofIreland",
"RepublicofMaldives", "Romania", "Russia", "Rwanda", "Samoa",
"SanMarino", "SaudiArabia", "Scotland", "Senegal",
"Seychelles", "SierraLeone", "Singapore", "Slovakia",
"Slovenia", "SolomonIslands", "Somalia", "SouthAfrica",
"SouthKorea", "Spain", "SriLanka", "Sudan", "Surinam",
"Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan",
"Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga",
"TrinidadandTobago", "Tunisia", "Turkey", "Turkmenistan",
"Tuvalu", "Uganda", "Ukraine", "UnitedArabEmirates",
"UnitedKingdom", "UnitedStatesofAmerica", "Uruguay",
"Uzbekistan", "Vanuatu", "VaticanCity", "Venezuela", "Vietnam",
"Wales", "Yemen", "Yugoslavia(SerbiaandMontenegro)", "Zambia",
"Zimbabwe" };
return countries;
}
}