焦点问题EditText

本文介绍如何通过设置父布局属性使EditText失去焦点,同时解释了android:focusable与android:focusableInTouchMode的区别及其应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使Edittext失去焦点,只需使其父布局获取焦点即可

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@null"
            android:text="搜索你感兴趣的内容..."
            android:singleLine="true"
             android:textSize="14sp"/>
     </RelativeLayout>

代码中使其父布局获取焦点:

            mTitle.setFocusableInTouchMode(true);
            mTitle.requestFocus();

android:focusable和android:focusableInTouchMode的区别:

android:focusableInTouchMode
点击屏幕的上的某个控件时,不要立即执行相应的点击逻辑,而是先显示焦点(即控件被选中),再点击才执行逻辑。


android:focusable=“true”不会改变android:focusableInTouchMode,因此只在键盘状态下显示焦点,在TouchMode状态下,依旧无法显示焦点。

android:focusable=“false”,一定会使android:focusableInTouchMode=“false”。

相对的

android:focusableInTouchMode=“false”,不会影响android:focusable。

android:focusableInTouchMode=”true”,一定会是android:focusable=“true”

如果要获取焦点只需要设置

android:foucusableInTouchMode=“true”



所有的获取焦点,都要有一个前提,那就是该控件必须设置android:clickable=”true”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zz白龙

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

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

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

打赏作者

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

抵扣说明:

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

余额充值