AutoCompleteTextView的使用举例

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

代码如下:

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);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值