Android SearchView设置与用法的那点事儿
2024-08-26 04:17:51
// 设置该SearchView默认是否自动缩小为图标
mSearchView
.setIconifiedByDefault(
false
);
// 为该SearchView组件设置事件监听器
mSearchView
.setOnQueryTextListener(
this
);
// 设置该SearchView显示确认搜索按钮
mSearchView
.setSubmitButtonEnabled(
true
);
// 设置该SearchView内默认显示的提示文本
mSearchView
.setQueryHint(
"查找"
);
//设置
mSearchView
.setIconified(false);
//清除焦点
mSearchView
.clearFocus();
//获取焦点
mSearchView.requestFocus();
EditText默认自动获取焦点的,会弹出软键盘
下面是关闭SearchView自动获取焦点的代码
<span style="font-size:14px;"><span style="font-size:14px;"> <LinearLayout
android:id="@+id/focus"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#EAEAEA"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:orientation="horizontal" > <SearchView
android:id="@+id/searchView"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:gravity="left|center_vertical"
android:iconifiedByDefault="false"
android:inputType="textFilter"
android:queryHint="输入IP"
android:textColor="#ABABAB"
android:textColorHint="#ABABAB" /> </LinearLayout></span></span>
只需要在SearchView的父级控件中添加以下属性:
<span style="font-size:14px;"><span style="font-size:14px;">android:focusable="true"
android:focusableInTouchMode="true"</span></span>
就在进入的时候不会自动获取焦点
但是当你点击SearchView获取焦点后,到别的activity再回来的时候,失效了,总是自动获取焦点并且弹出软键盘。
这里有个办法就是在onResume方法里添加以下代码
<span style="font-size:14px;"><span style="font-size:14px;">@Override
protected void onResume() {
super.onResume();
mSearchView.setFocusable(true);
mSearchView.setFocusableInTouchMode(true);
//mSearchView.requestFocus(); //获取焦点
}</span></span>
回到这个activity会执行onResume
方法,让它执行上面的代码,就不再会自动获取焦点了。
最新文章
- nopcommerce之一(结构分析)
- jquery工具方法makeArray/merge
- 数据库中 dbo是什么意思
- PHP访问REST API上传文件的解决方案
- Python 异常结构
- GridView下DropDownList 的选择方法onselectedindexchanged 实现方法
- 转载 Eclipse下的SSH开发例子
- Java IO 和 NIO
- TCP四次挥手
- swift3.0 基础练习-实现99乘法表
- Python爬虫实例:爬取豆瓣Top250
- Apache Flink系列(1)-概述
- 判断浏览器大于等于ie9
- jQuery中删除方法empty(),remove()和detach()的区别
- JMeter实现登录初始化(类似LR的init函数功能实现)
- Python 第五阶段 学习记录之---Django 基础
- WorkerMan 入门学习之(三)基础教程-Timer类的使用
- 随笔:JS对象无new构造原理
- CSS的再深入2(更新中&#183;&#183;&#183;)
- 前端-CSS-2-选择器