Android 程式开发:(十二)最基本的UI控件 —— 12.4AutoCompleteTextView

AutoCompleteTextView和EditText很相似,事实上,AutoCompleteTextView就是EditText的子类。使用AutoCompleteTextView,当用户正在输入时,会自动弹出一些提示信息。下面的例子将会展示如何使用AutoCompleteTextView去自动地帮助用户完成输入。

1、创建一个工程:BasicViews3。

Android 程式开发:(十二)最基本的UI控件 —— 12.4AutoCompleteTextView

2、main.xml中的代码。

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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="Name of President" /> <AutoCompleteTextView android:id="@+id/txtCountries" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>3、BasicViews3Activity.java中的代码。

package net.learn2develop.BasicViews3; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class BasicViews3Activity extends Activity { String[] presidents = { "Dwight D. Eisenhower", "John F. Kennedy", "Lyndon B. Johnson", "Richard Nixon", "Gerald Ford", "Jimmy Carter", "Ronald Reagan", "George H. W. Bush", "Bill Clinton", "George W. Bush", "Barack Obama" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, presidents); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries); textView.setThreshold(3); textView.setAdapter(adapter); } }4、F11调试。

Android 程式开发:(十二)最基本的UI控件 —— 12.4AutoCompleteTextView