Android中如何取消调转界面后EditText默认获取聚焦问题
最近在做一个项目,当我点击跳转至一个带有EditText的界面后,模拟器中的软键盘会自动弹出,严重影响了用户体验。在网上找了资料,现总结如下。
我们知道,EditText有一个 android:focusable=""的属性,但是如果你在edittext中直接将这个属性设置为true的话,点进去软键盘确实不会再弹出,但是EditText相应的也失去了聚焦,即无论你怎么点击它都不会有反应,这也就失去了EditText的原本的作用。那么要解决这个问题其实很简单,只需在EditText的父控件中加上这两行代码即可:
android:focusable="true"
android:focusableInTouchMode="true"
完整布局:
<RelativeLayout
android:background="@drawable/holiday_search"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginLeft="50dp"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:focusable="true"
android:focusableInTouchMode="true"
>
<ImageView
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:layout_width="20dp"
android:layout_height="20dp"
android:gravity="center_vertical"
android:background="@drawable/abs__ic_search"
/>
<EditText
android:singleLine="true"
android:textSize="13sp"
android:hint="输入景点名或城市名"
android:background="#00ff00ff"
android:gravity="center_vertical"
android:layout_alignParentLeft="true"
android:layout_marginLeft="30dp"
android:layout_width="fill_parent"
android:layout_height="30dp"
/>
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:background="@drawable/abs__ic_voice_search_api_holo_light"
/>
</RelativeLayout>
那么为什么这样加就没有问题了呢?我们知道父控件实际上并不需要获得聚焦,我们将父控件的focusable设置为false之后就使得整个父控件属于失去聚焦的状态,当我们点击跳转时软键盘也就不会再自动弹出,相应的,当我们需要对EditText进行编辑时也不会有影响。
最新文章
- 学习 opencv---(6)玩转opencv源代码:生成opencv 工程解决方案与opencv 源码编译
- 仿【Emmet】转【HTML】功能
- JMeter学习-020-JMeter 监听器之【聚合报告】错误率、吞吐量、传输速率实例计算
- phpmailer 参数使用说明
- (VS) TFS lost mapping suddenly.
- Winform开发几个常用的开发经验及知识积累(一)
- Linux下oracle导入(exp)导出(imp)出现";Failed to open ...for reader/write";错误
- 运算符优先级 (Transact-SQL)
- Hadoop MapReduce编程的一些个人理解
- Java中读取某个目录下的所有文件和文件夹
- 实战-Mysql主从复制
- linux的定时任务服务crond(crontab)服务
- ejs常用语法
- MySQL执行语句的顺序
- hibernate多对多映射文件的配置
- linux下对clamav杀毒软件的安装和配置
- angularjs的一些问题
- js日期的格式化
- with 重写enter exit 方法
- (转)SQL知识_Sql日期时间格式转换