AutoCompleteTextView和EditText很相似,事实上,AutoCompleteTextView就是EditText的子类。使用AutoCompleteTextView,当用户正在输入时,会自动弹出一些提示信息。下面的例子将会展示如何使用AutoCompleteTextView去自动地帮助用户完成输入。
1、创建一个工程:BasicViews3。
2、main.xml中的代码。
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="NameofPresident"/>
- <AutoCompleteTextViewandroid:id="@+id/txtCountries"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- packagenet.learn2develop.BasicViews3;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.widget.ArrayAdapter;
- importandroid.widget.AutoCompleteTextView;
- publicclassBasicViews3ActivityextendsActivity{
- String[]presidents={
- "DwightD.Eisenhower",
- "JohnF.Kennedy",
- "LyndonB.Johnson",
- "RichardNixon",
- "GeraldFord",
- "JimmyCarter",
- "RonaldReagan",
- "GeorgeH.W.Bush",
- "BillClinton",
- "GeorgeW.Bush",
- "BarackObama"
- };
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ArrayAdapter<String>adapter=newArrayAdapter<String>(this,
- android.R.layout.simple_dropdown_item_1line,presidents);
- AutoCompleteTextViewtextView=(AutoCompleteTextView)
- findViewById(R.id.txtCountries);
- textView.setThreshold(3);
- textView.setAdapter(adapter);
- }
- }
