避免 EditText 弹出软键盘的处理方法

本文介绍了在Android应用中防止EditText启动时自动弹出软键盘的四种方法,并提供了调整软键盘显示模式避免遮挡EditText的有效策略。

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

不知道大家是否有过这样的经历,那就是在一个activity中放置一个EditText,当启动Activity的时候,通常情况下会弹出软键盘,非常影响用户体验,为了避免这种情况发生,经过总结,有以下方法可以解决这个问题:

解决方法1:

在activity属性中设置 android:windowSoftInputMode=”stateHidden”

解决方法2:

系统默认第一个EditText是获得焦点的,解决办法,增加一个不显示的view强制获得焦点,比如:

 <View
        android:layout_width="0dip"
        android:layout_height="0dip"
        android:focusableInTouchMode="true" />

解决方法3:

InputMethodManager inputMethodManager = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(et.getWindowToken(), 0);

解决方法4:类似与第二个方法

在其父控件下,添加如下的属性,就可以完美解决:

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

举例如下:



<LinearLayout
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/et_enter_msg_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="wrap_content" />

    <Button
        android:id="@+id/sent"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/send" />

</LinearLayout>

关于弹出软键盘遮盖EditText的处理办法:
[“stateUnspecified”, “stateUnchanged”,
“stateHidden”, “stateAlwaysHidden”, “stateVisible”,
“stateAlwaysVisible”, “adjustUnspecified”,
“adjustResize”, “adjustPan”] >
通常情况下,只需要在AndroidMenifest.xml的Activity根节点下添加:activity android:windowSoftInputMode=adjustResize 属性即可,全屏状态下可使用:activity android:windowSoftInputMode=adjustPan 属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值