前言
提到android的动态搜索,大多应该会想到edittext的文本改变的监听器(addtextchangedlistener),本文会简单介绍一下,但是本文介绍的是searchview+listview的实现。
效果图:
一、addtextchangedlistener
使用这种方式的思路简述就是,当监听到文本改变时,就用handler post一个runnable去做相应的改变,动态修改listview的显示。
二、本文案例
1.介绍一下searchview的一些方法
- seticonified():设置搜索框直接展开显示。左侧有放大镜(在搜索框中) 右侧有叉叉 可以关闭搜索框
- seticonifiedbydefault():设置搜索框直接展开显示。左侧有放大镜(在搜索框外) 右侧无x样式点击按钮 有输入内容后有x样式点击按钮 不能关闭搜索框
- onactionviewexpanded():设置搜索框直接展开显示。左侧有无放大镜(在搜索框中) 右侧无叉叉 有输入内容后有x样式点击按钮, 不能关闭搜索框
- setonquerytextlistener():为 searchview 中的用户操作设置侦听器。
- setsubmitbuttonenabled():当查询非空时启用显示提交按钮。
- setqueryhint():查询提示语句
2.准备数据
本案例使用一个string数组
private final string[] mstrings = cheeses.scheesestrings;
3.初始化以及填充数据
msearchview = (searchview) findviewbyid(r.id.search_view); mlistview = (listview) findviewbyid(r.id.list_view); mlistview.setadapter(madapter = new arrayadapter<>(this, android.r.layout.simple_list_item_1, mstrings)); //设置是否可以通过键盘输入的字符来过滤掉不需要的选项,定位到需要的选项。 mlistview.settextfilterenabled(true); setupsearchview();
private void setupsearchview() { //设置搜索框直接展开显示。左侧有放大镜(在搜索框中) 右侧有叉叉 可以关闭搜索框 //msearchview.seticonified(false); //设置搜索框直接展开显示。左侧有放大镜(在搜索框外) 右侧无叉叉 有输入内容后有叉叉 不能关闭搜索框 //msearchview.seticonifiedbydefault(false); //设置搜索框直接展开显示。左侧有无放大镜(在搜索框中) 右侧无叉叉 有输入内容后有叉叉 不能关闭搜索框 msearchview.onactionviewexpanded(); //为 searchview 中的用户操作设置侦听器。 msearchview.setonquerytextlistener(this); //当查询非空时启用显示提交按钮。 msearchview.setsubmitbuttonenabled(false); //查询提示语句 msearchview.setqueryhint(getstring(r.string.cheese_hunt_hint)); }
4.在searchview中用户输入字符时激发方法里写入简单逻辑
//用户输入字符时激发该方法 public boolean onquerytextchange(string newtext) { if (textutils.isempty(newtext)) { mlistview.cleartextfilter(); } else { mlistview.setfiltertext(newtext.tostring()); } return true; }
三、源码
jimengsearchview.java
public class jimengsearchview extends activity implements searchview.onquerytextlistener { private searchview msearchview; private listview mlistview; private arrayadapter<string> madapter; private final string[] mstrings = cheeses.scheesestrings; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getwindow().requestfeature(window.feature_action_bar); setcontentview(r.layout.searchview_filter); msearchview = (searchview) findviewbyid(r.id.search_view); mlistview = (listview) findviewbyid(r.id.list_view); mlistview.setadapter(madapter = new arrayadapter<>(this, android.r.layout.simple_list_item_1, mstrings)); //设置是否可以通过键盘输入的字符来过滤掉不需要的选项,定位到需要的选项。 mlistview.settextfilterenabled(true); setupsearchview(); mlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string str = (string)((textview) view).gettext(); toast.maketext(jimengsearchview.this,str,toast.length_short).show(); } }); } private void setupsearchview() { //设置搜索框直接展开显示。左侧有放大镜(在搜索框中) 右侧有叉叉 可以关闭搜索框 //msearchview.seticonified(false); //设置搜索框直接展开显示。左侧有放大镜(在搜索框外) 右侧无叉叉 有输入内容后有叉叉 不能关闭搜索框 //msearchview.seticonifiedbydefault(false); //设置搜索框直接展开显示。左侧有无放大镜(在搜索框中) 右侧无叉叉 有输入内容后有叉叉 不能关闭搜索框 msearchview.onactionviewexpanded(); //为 searchview 中的用户操作设置侦听器。 msearchview.setonquerytextlistener(this); //当查询非空时启用显示提交按钮。 msearchview.setsubmitbuttonenabled(false); //查询提示语句 msearchview.setqueryhint(getstring(r.string.cheese_hunt_hint)); } //用户输入字符时激发该方法 public boolean onquerytextchange(string newtext) { if (textutils.isempty(newtext)) { mlistview.cleartextfilter(); } else { mlistview.setfiltertext(newtext.tostring()); } return true; } public boolean onquerytextsubmit(string query) { return false; } }
布局文件:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <searchview android:id="@+id/search_view" android:layout_width="match_parent" android:layout_height="wrap_content"/> <listview android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> </linearlayout>
strings.xml
<string name="cheese_hunt_hint">请输入要查询的内容</string>
cheeses.java
public class cheeses { public static final string[] scheesestrings = { "android自定义view之3d正方体","计蒙不吃鱼","android自定义view之利用drawarc方法实现动态效果","android 3d效果的实现","okhttp源码解析", "android翻转动画(卡片翻转效果)","android自定义view之围棋动画","android自定义view之模仿登录界面文本输入框(华为云app)", "android自定义view之太极图","android自定义view获取attr中自定义颜色的问题","android对抗反编译","android常用的room增删改查语句(外部数据库)", "android用canvas画一个折线图,并加以简单封装","android用canvas画一个真正能跑的跑马灯","android网络小说阅读器的实现", "android护眼模式(argb)","android约束布局constraintlayout","android实现edittext的抖动效果" }; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。