不让editText自动获取焦点

本文讨论了如何在Android应用中通过调整父控件属性来防止EditText组件在加载布局时自动获取焦点的问题,提供了具体实现方法并进行了验证。

一个布局里有edittext的话 一加载这个布局 edittext就会自动获取焦点 很烦

之前在网上查了一下 怎么不让edittext自动获取焦点

有位大神写到 在它的父控件上加上这样两个参数:

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

自己试了一下 确实管用 写上之后edittext就失去焦点了 除非你点击它 要不然它是不会获取焦点的

但是如果我把这两句话写到其他的地方呢?还管用吗?测下

假如我有一个textview和一个edittext 都有自己的父控件 我把这两句话写到textview的父控件里

结果是一样的 edittext同样会失去焦点

经过一番测试后得到结论 只要把这两句话写到任何一个父控件中 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:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="aaaaaaaa" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="bbbbbbbbb" />
    </LinearLayout>

</LinearLayout>

像上面的布局文件里有三个linearlayout 

把android:focusable="true"
android:focusableInTouchMode="true"插入到任何一个linearlayout里都会是edittext失去焦点

但是插入到textview里则不行

转载于:https://www.cnblogs.com/context/p/4848979.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值