代码如下:
AppMain.java
package lxy.litsoft;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AppMain extends Activity {
AutoCompleteTextView autoCompleteTextView;
List<String> list;
ArrayAdapter<String> arrayAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//实例化AutoCompleteTextView对象
autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autoComplete);
//实例化List对象并添加item
list = new ArrayList<String>();
list.add("liu_zhen_wei");
list.add("liu_zhen_wei_01");
list.add("liu_zhen_wei_02");
list.add("wang1");
list.add("wang2");
list.add("huohuo");
//实例化ArrayAdapter对象为AutoCompleteTextView提供数据
arrayAdapter = new ArrayAdapter<String>(this,R.layout.item,list);
//为AutoCompleteTextView添加适配器
autoCompleteTextView.setAdapter(arrayAdapter);
}
}main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_marginTop="100dip"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" ></AutoCompleteTextView>
</LinearLayout>
item.mxl
<?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"
/>
创建一个AutoCompleteTextView的步骤
1.在布局文件中声明一个AutoCompleteTextView
2.在res/layout文件夹下新建一个布局文件,名为list_item.xml确定AutoComleteTextView的提示样式
3.创建一个ArrayAdapter为AutoCompleteTextView提供数据
List<String> list = new ArrayList<String>();
list.add("abcd");
list.add("abdd");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,R.layout.item,list);
4.为AutoCompleteTextView添加适配器
autoCompleteTextView.setAdapter(array Adapter);

本文介绍如何使用 Android 中的 AutoCompleteTextView 控件实现自动完成文本输入功能,包括创建控件、设置样式及绑定数据源等步骤。
8527

被折叠的 条评论
为什么被折叠?



