Android用户输入自动提示控件AutoCompleteTextView使用方法
一、简介
1、autocompletetextview的作用
2、autocompletetextview的类结构图
也就是拥有edittext的各种功能
3、autocompletetextview工作原理
autocompletetextview的自动提示功能肯定需要适配器提供数据
4、android里的适配器
5、适合autocompletetextview的适配器
arrayadapter
二、autocompletetextview实现自动提示的方法
1)autocompletetextview实现自动提示的方法
第一步、创建适配器
string[] arr={"凯撒","凯撒广场","凯撒大帝"};
arrayadapter<string> adapter= new arrayadapter<string>(this, r.layout.textview, arr);
第二步、autocompletetextview对象应用适配器
autocompletetextview1.setadapter(adapter);
说明:
提示文本是用textview实现的,提示文本里面的提示数据就是string[] arr。
三、代码实例
代码:
fry.activity01
package fry; import com.example.autocompletetextviewdemo1.r; import android.app.activity; import android.os.bundle; import android.widget.arrayadapter; import android.widget.autocompletetextview; public class activity01 extends activity{ private autocompletetextview autocompletetextview1; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.activity01); autocompletetextview1=(autocompletetextview) findviewbyid(r.id.autocompletetextview1); /* * autocompletetextview实现自动提示的方法 * 第一步、创建适配器 * 第二步、autocompletetextview对象应用适配器 * */ string[] arr={"凯撒","凯撒广场","凯撒大帝"}; arrayadapter<string> adapter= new arrayadapter<string>(this, r.layout.textview, arr); autocompletetextview1.setadapter(adapter); } }
fry.activity01
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <autocompletetextview android:id="@+id/autocompletetextview1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </linearlayout>
/autocompletetextviewdemo1/res/layout/textview.xml
<?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 这里是直接textview,而不是layout下的textview --> </textview >
四、易错点
1、这里是直接textview,而不是layout下的textview
/autocompletetextviewdemo1/res/layout/textview.xml
<?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 这里是直接textview,而不是layout下的textview --> </textview >
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 人生日历整点报时/定时关机功能使用教程