您好,欢迎访问一九零五行业门户网

Android UI控件系列:AutoCompleteTextView(自动提示)

autocompletetextview的功能类似于百度或者google在搜索栏输入信息的时候,弹出的与输入信息接近的提示信息。当然这里要要用到一些适配器
在android中提供了两智能输入框,它们是multiautocompletetextview、autocompletetextview。它们的功能大致一样,它和autocompletetextview的区别就是multiautocompletetextview可以在输入框中一直增加新的选取值。编写方式也有所不同,在进行setadapter之后还需要调用settokenizer() 。下面详细介绍一下。
一、autocompletetextview
1.简介
一个可编辑的文本视图,当用户输入信息后弹出提示。提示列表显示在一个下拉菜单中,用户可以从中选择一项,以完成输入。提示列表是从一个数据适配器获取的数据。
2.重要方法
clearlistselection():清除选中的列表项
dismissdropdown():如果存在关闭下拉菜单
getadapter():获取适配器
3.创建须知
(1)布局文件
<autocompletetextview android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" />
(2)程序
实例化适配器
arrayadapter<string> adapter = new arrayadapter<string>(this,android.r.layout.simple_dropdown_item_1line, strs);
设置适配器
edit.setadapter(adapter);
二、multiautocompletetextview
1.简介
继承自autocompletetextview,延长autocompletetextview的长度,你必须要提供一个multiautocompletetextview.tokenizer来区分不同的子串
2.重要方法
enoughtofilter():当文本长度超过阈值时过滤
performvalidation():代替验证整个文本,这个子类方法验证每个单独的文字标记
settokenizer(multiautocompletetextview.tokenizer t);用户正在输入时,tokenizer设置将用于确定文本相关范围内
3.使用须知
(1)布局文件
<multiautocompletetextview android:id="@+id/edit1" android:layout_width="match_parent" android:layout_height="wrap_content" />
(2)程序
实例化适配器
arrayadapter<string> adapter = new arrayadapter<string>(this,android.r.layout.simple_dropdown_item_1line, strs);
确定范围
edit1.settokenizer(new multiautocompletetextview.commatokenizer())
下面是个例子
autocommittest.java
package org.hualang.auto; import android.app.activity; import android.os.bundle; import android.widget.arrayadapter; import android.widget.autocompletetextview; import android.widget.multiautocompletetextview; public class autocommittest extends activity { /** called when the activity is first created. */ private static final string[] autostring=new string[]{"welcome","well", "weatch","weexeview","werap"}; @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, autostring); autocompletetextview autocomplete=(autocompletetextview)findviewbyid(r.id.auto); autocomplete.setadapter(adapter); multiautocompletetextview multi=(multiautocompletetextview) findviewbyid(r.id.multi); //将adapter添加到autocompletetextview中 multi.setadapter(adapter); multi.settokenizer(new multiautocompletetextview.commatokenizer()); } }
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" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自动提示功能演示" /> <autocompletetextview android:id="@+id/auto" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <multiautocompletetextview android:id="@+id/multi" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </linearlayout>
运行结果如下:
以上就是android ui控件系列:autocompletetextview(自动提示)的内容。
其它类似信息

推荐信息