android 搜索框实现方法,还在手写搜索框?out了!附加Toast及时改变内容
来源:互联网 发布:猎豹浏览器mac版下载 编辑:程序博客网 时间:2024/05/29 14:19
效果图:
教程::
如图,之前用到搜索框都是自己写,里面的EditText还得设置内容改变监听来实现实时搜索,偶然间发现V7包中存在有搜索控件
<android.support.v7.widget.SearchView android:id="@+id/searchview" android:layout_width="match_parent" android:layout_height="56dp" android:focusable="false" android:focusableInTouchMode="false" android:background="@drawable/seatch_bj" //控件整体的背景 app:iconifiedByDefault="false" app:queryBackground="@drawable/seatch_et_bj" //搜索框的背景 app:queryHint="请输入关键字" android:textColor="#000000"/>
然后设置个监听就OK啦:
searchview = (SearchView) findViewById(R.id.searchview);searchview.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } //搜索框内部改变回调,newText就是搜索框里的内容 @Override public boolean onQueryTextChange(String newText) { thisToast(newText); //这里面就是一个Toast return true; }});
搜索框也可以根据自己的爱好自行定义,比如背景:
不知这些,还有很多自定义的属性可以设置,例如:
有上图可以看出,有内边距啊、搜索图标啊……都可以根据自己想要的自行定义,好了,就这些,由于代码比较简单,就不放源码了
拓展:平常用的Toast一般都是2秒才能显示下一条Toast,而动态图中的Toast可以随时更改内容,实现起来比较简单:
private Toast toast;private void thisToast(final String msg){ if(toast == null){ toast = Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT); } toast.setText(msg); toast.show(); }
0 0
- android 搜索框实现方法,还在手写搜索框?out了!附加Toast及时改变内容
- 还在手动记录数据?那你就OUT了!
- 实现第一条Toast还没显示完直接显示第二条内容,改变Toast内容
- android搜索框实现
- android搜索框实现
- android搜索框实现
- android中实现内容搜索
- android中实现内容搜索
- 写论文 找文献资料 你还在用网页搜索吗?你OUT了(转自:http://emuch.net/html/201203/4144738.html)
- Android还在用Toast?你Out啦,该试试Snackbar了
- android搜索框实现 【转】
- android搜索框功能实现
- Android搜索框输入内容点击键盘的搜索按钮进行搜索
- dataTable 改变搜索框位置
- 搜索框控件,写了个最简单的dome
- Android----- 改变图标原有颜色 和 搜索框
- 实现搜索框提示功能的方法
- 百度搜索框内容实现微博同步转发
- zzulioj-1899
- 设置模式-创建型
- StringTokenizer类的使用
- 系统调用的实现原理
- iOS TabBar高度自动会变高
- android 搜索框实现方法,还在手写搜索框?out了!附加Toast及时改变内容
- html/css手机网站基础代码
- Python在自动化运维时经常会用到的方法
- fragment懒(可见时)加载
- c语言实现由网络地址获取ip
- const char*、char*、char* const、char[]、string的区别及其转换
- Android小技巧之来不及解释了快上车--EventBus3
- VS2013 MFC工程 调试DLL出现: 当前不会命中断点,还没有为该文档加载任何符号
- 当点击back键和home键时activity的生命周期