Android取消Edittext默认获取焦点

Android取消Edittext默认获取焦点

Android控件EditText自动默认会获取屏幕焦点,并显示输入键盘,有时候我们需要取消这个行为,

只需要两行代码,如下:

android:focusable="true"  
android:focusableInTouchMode="true" 

我们只需要把这两行代码添加至EditText控件的父级控件中,把屏幕焦点设置到父级控件上,EditText就会暂时失去焦点。

<LinearLayout  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:focusable="true"  
    android:focusableInTouchMode="true"  
    android:orientation="vertical" >  

    <EditText  
        android:id="@+id/et_user_name"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content" />  
</LinearLayout>  

万一我们的页面中有多个Edittext呢?
其实可以放在多级父控件中,甚至放在根目录的布局中都是可以实现:取消获取子控件(或多级子控件)默认获取焦点。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

....//各种子控件

</LinearLayout>

这里不用担心以后不会重新获得焦点,上面两句修改只是让默认显示的情况不获取焦点,当你点击Edittext的时候,还是会有焦点,并显示虚拟键盘的。

共勉:没有伞的孩子更应该学会在雨中奔跑

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

峥嵘life

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值