Android开发之EditText自动获取焦点自动弹出软键盘的解决办法

先看下如何自动弹出软键盘:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F0F2F5"
        android:orientation="vertical">

        <include layout="@layout/common_title_view" />
        <!--下面两个属性禁止editText自动弹出键盘-->
        <LinearLayout
            style="@style/ll_common_layout_style"
            android:focusable="true"
            android:focusableInTouchMode="true">

            <TextView
                style="@style/tv_common_collage_show_style"
                android:text="商品名称" />

            <EditText
                style="@style/et_collage_input_style"
                android:hint="请输入商品名称" />
        </LinearLayout>

        <LinearLayout style="@style/ll_common_layout_style">

            <TextView
                style="@style/tv_common_collage_show_style"
                android:text="截止时间" />

            <EditText
                style="@style/et_collage_input_style"
                android:drawableRight="@mipmap/ic_more"
                android:hint="请选择截止时间"
                android:paddingRight="@dimen/dp_10" />
        </LinearLayout>

        <LinearLayout style="@style/ll_common_layout_style">

            <TextView
                style="@style/tv_common_collage_show_style"
                android:text="商品单价" />

            <EditText
                style="@style/et_collage_input_style"
                android:hint="¥ 请输入商品单价" />
        </LinearLayout>

        <LinearLayout style="@style/ll_common_layout_style">

            <TextView
                style="@style/tv_common_collage_show_style"
                android:text="卖家输入地址" />

            <EditText
                style="@style/et_collage_input_style"
                android:hint="请输入地址" />
        </LinearLayout>

        <LinearLayout style="@style/ll_common_layout_style">

            <TextView
                style="@style/tv_common_collage_show_style"
                android:text="设置自提地址" />

            <EditText
                style="@style/et_collage_input_style"
                android:hint="请输入地址" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_42"
            android:background="@drawable/add_shop_shape_bg"
            android:gravity="center">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:drawableLeft="@mipmap/addsm"
                android:drawablePadding="@dimen/dp_9"
                android:gravity="center"
                android:text="添加新商品" />
        </LinearLayout>

        <LinearLayout style="@style/ll_common_layout_style">

            <TextView
                style="@style/tv_common_collage_show_style"
                android:text="0人成团" />

            <TextView
                style="@style/addAndDelete"
                android:text="—" />

            <TextView
                style="@style/addAndDelete"
                android:layout_marginLeft="-1dp"
                android:layout_marginRight="@dimen/dp_10"
                android:background="@drawable/collage_add_shape"
                android:text="+" />
        </LinearLayout>

        <LinearLayout style="@style/ll_common_layout_style">

            <TextView
                style="@style/tv_common_collage_show_style"
                android:text="0件成团" />

            <TextView
                style="@style/addAndDelete"
                android:text="—" />

            <TextView
                style="@style/addAndDelete"
                android:layout_marginLeft="-1dp"
                android:layout_marginRight="@dimen/dp_10"
                android:background="@drawable/collage_add_shape"
                android:text="+" />
        </LinearLayout>


        <LinearLayout style="@style/ll_common_layout_style">

            <TextView
                style="@style/tv_common_collage_show_style"
                android:text="每人每单最多" />

            <TextView
                style="@style/et_collage_input_style"
                android:layout_marginRight="@dimen/dp_10"
                android:drawablePadding="@dimen/dp_5"
                android:drawableRight="@mipmap/ic_more"
                android:gravity="right"
                android:text="99件"
                android:textColor="@color/black" />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/dp_5"
            android:layout_marginLeft="@dimen/dp_15"
            android:layout_marginTop="@dimen/dp_5"
            android:text="添加图片(1件商品1张图片,按顺序上传)"
            android:textColor="#999999" />


        <RelativeLayout
            android:id="@+id/ll_add_image"
            android:layout_width="@dimen/dp_106"
            android:layout_height="@dimen/dp_85"
            android:layout_marginBottom="@dimen/dp_5"
            android:layout_marginTop="@dimen/dp_5">

            <ImageView
                android:id="@+id/iv_set_image"
                android:layout_width="@dimen/dp_99"
                android:layout_height="@dimen/dp_78"
                android:layout_marginRight="@dimen/dp_7"
                android:layout_marginTop="@dimen/dp_7"
                android:src="@mipmap/add_image" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:src="@mipmap/delete" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_1"
            android:background="@color/white"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="@dimen/dp_42"
                android:gravity="center"
                android:paddingLeft="@dimen/dp_15"
                android:text="商品介绍"
                android:textColor="@color/black"
                android:textSize="14sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_1"
                android:background="#F0F2F5" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_84"
                android:background="@android:color/transparent"
                android:hint="请输入商品介绍"
                android:paddingBottom="@dimen/dp_15"
                android:paddingLeft="@dimen/dp_15"
                android:textColor="#333333"
                android:textSize="14sp" />
        </LinearLayout>

        <LinearLayout style="@style/ll_common_layout_style">

            <TextView
                style="@style/tv_common_collage_show_style"
                android:text="售后电话"
                android:textColor="@color/black" />

            <EditText
                style="@style/et_collage_input_style"
                android:hint="请输入售后电话" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>

如上代码:在EditText布局的父类布局的xml中设置如下即可:

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

就可以解决了

我们看下添加属性后的效果:

感谢博主:原博主链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
EditText默认获取焦点弹出软键盘,在Android开发中非常常见和实用。当我们在布局文件中使用EditText控件时,默认情况下,该控件会自动获取焦点弹出软键盘。 这种默认行为可以方便用户直接对EditText进行输入操作,提高用户的交互体验。当用户点击EditText或切换到包含EditText的布局时,EditText自动获得焦点,这样用户可以直接进行输入操作。 在代码中,我们可以通过设置android:focusable属性为true来保证EditText默认获取焦点。同时,还可以设置android:focusableInTouchMode属性为true,确保用户触摸到屏幕上的EditText时,它会获取焦点弹出软键盘。 例如,在布局文件中可以这样设置: <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:focusableInTouchMode="true" /> 这样,当布局加载完成后,EditText自动获取焦点,用户可以直接对其进行输入操作。 需要注意的是,如果你希望EditText不再获取焦点弹出软键盘,则可以将android:focusable和android:focusableInTouchMode属性设置为false。 尽管EditText默认获取焦点弹出软键盘在很多情况下非常方便,但有些场景可能不适用,比如登录界面中的密码框,我们可能更愿意让用户手动点击密码框来输入密码,而不是自动弹出软键盘。 总之,EditText默认获取焦点弹出软键盘是一个很方便的功能,可以提高用户的交互体验,但在一些特殊场景中需要谨慎使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值