免费赠书,崔晓,团购白酒
以在搜索框搜索时,自动补全为例:
其中还涉及到一个词,tokenizer:分词器,分解器。
上效果图:
mainactivity.java:
package com.joan.testautocomletetextview; import android.r.array; import android.os.bundle; import android.app.activity; import android.content.res.resources; import android.view.menu; import android.widget.arrayadapter; import android.widget.autocompletetextview; import android.widget.multiautocompletetextview; import android.widget.multiautocompletetextview.tokenizer; public class mainactivity extends activity { autocompletetextview actv; multiautocompletetextview mactv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); actv = (autocompletetextview) findviewbyid(r.id.actv); mactv = (multiautocompletetextview) findviewbyid(r.id.mactv); // 取到strings.xml中定义的数组 string[] names = this.getresources().getstringarray(r.array.names); // 适配器 // 第三个参数是数据源 // 第二个参数是样式资源的id arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_expandable_list_item_1, names); // =============只能选择单个的自动补全===================== actv.setadapter(adapter); // =============可选择多个的自动补全===================== // tokenizer分词器,分解器 // multiautocompletetextview.commatokenizer();这个简易的分解器可用于对由逗号和若干空格分割的列表进行分解. tokenizer t = new multiautocompletetextview.commatokenizer(); mactv.setadapter(adapter); mactv.settokenizer(t); } }
strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">testautocompletetextview</string> <string name="action_settings">settings</string> <string name="hello_world">hello world!</string> <!--定义一个数组 --> <string-array name="names"> <item >zhangyu</item> <item >zhangxinzhe</item> <item >zhangxingxing</item> <item >liudehua</item> <item >liuyi</item> </string-array> </resources>
activity_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="只可选择单个" /> <!--autocompletetextview 自动补全,只能选择一个值 android:completionthreshold="1" 输入第一个字后自动补全 --> <autocompletetextview android:id="@+id/actv" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入搜索的名字" android:completionthreshold="1" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="可选择多个" /> <!--multiautocompletetextview 可以选择多个值 --> <multiautocompletetextview android:id="@+id/mactv" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入搜索的名字" android:completionthreshold="1" /> </linearlayout>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android apk 项目一键打包并上传到蒲公英的实现方法
Android 自定义LineLayout实现满屏任意拖动功能的示例代码
android 限制某个操作每天只能操作指定的次数(示例代码详解)
Android 集成 google 登录并获取性别等隐私信息的实现代码
网友评论