禁止EditView自动弹出系统软键盘_Android基础篇(Java)

文 | Promise Sun


1.场景描述:

1) APP项目中经常会开发自定义软键盘;同时在使用EditView时,也会常常遇到自动弹出系统自带的软键盘,与自定义的软键盘产生冲突的情况;此时需要禁止EditView自动弹出系统软键盘,从而使自定义的软键盘正常显示。

2) 正常的禁用系统软键盘方法无法满足需求,eg:在复制粘贴文本前,选中EditView中的文本时,Android系统会自动调起软键盘,与自定义的软键盘产生冲突;此时需要特殊处理一下。
希望达到的效果是无论如何点击EditView,系统软键盘绝对不会弹出来——实现彻底禁止EditView自动弹出系统软键盘。

2. 功能实现

1)首先,在清单文件AndroidManifest.xml中,给EditView所在的Activity页面设置属性

 android:windowSoftInputMode="stateHidden"

eg:(示例仅供参考)

 <activity
            android:name=".promise.sun.DemoActivity"
            android:windowSoftInputMode="stateHidden"/>

2)然后,找到EditView所在的xml布局文件,找到EditView的父布局(容器),设置属性

android:focusable="false"
android:focusableInTouchMode="false"

eg:(示例仅供参考)

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

                <EditText
                    android:id="@+id/et_search"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_35"
                    android:layout_gravity="center"
                    android:background="@drawable/bg_white_border_buleside_10"
                    android:drawableLeft="@drawable/icon_search"
                    android:drawableRight="@drawable/icon_clear"
                    android:hint="请输入"
                    android:maxLength="12"
                    android:maxLines="1"
                    android:paddingLeft="@dimen/dp_30"
                    android:paddingRight="@dimen/dp_10"
                    android:singleLine="true"
                    android:textColor="@color/app_black"
                    android:textColorHint="@color/grey_nine"
                    android:textColorHighlight="@color/blue_two"
                    android:textCursorDrawable="@drawable/cursor"
                    android:textSize="@dimen/sp_14" />
          </RelativeLayout>

3)最后,找到EditView所在的Activity,在初始化时设置方法

editView.setShowSoftInputOnFocus(false);

4)完美解决,亲测有效。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值