秒懂Android开发之 android:windowSoftInputMode 属性详解

【版权申明】非商业目的可自由转载
博文地址:https://blog.csdn.net/ShuSheng0007/article/details/104232176
出自:shusheng007

概述

曾几何时,你是否对软键盘的显示与隐藏,软键盘与页面布局的关系傻傻分不清,测试的小姐姐时不时就会过来抱怨,

王二狗:
“你这个键盘怎么把输入框盖住了,你让我怎么输入啊?“
“你这个键盘怎么把标题栏都顶没了?”
“产品要求一进入这个页面键盘就是要主动弹出来的,你这个没有弹啊?”
“产品要求一进入这个页面键盘是隐藏的,点击输入后才弹出,你这个为什么刚进来就弹出了呢?”

如果测试要真是个漂亮的小姐姐,倒也是一桩人间美事,说不定还能碰撞出爱情的小火花,那要是王姐,或者李姐之类胖大妈天天找你谈心,你是不是就要疯了?
所以,让我们一起来彻底搞懂android:windowSoftInputMode吧,把握主动权!

windowSoftInputMode

android:windowSoftInputMode 一般是配置在AndroidManifest.xml中来设置软键盘的行为的,当然也可以通过代码设置,如下所示

<activity android:name=".Activity2" android:windowSoftInputMode="stateUnspecified|adjustUnspecified"/>

android:windowSoftInputMode 的属性值竟然多达10种,真是丧心病狂,但是仔细研究后发现也不是那么不近人情,接下来就让我们秒懂它吧。

其属性分为两类:6个用来控制软键盘的可见状态,4个用来控制软键盘与视图window的布局关系。

软键盘可见性

假设有两个Activity, Activity1Activity2

属性值含义
stateUnspecified系统默认行为,如果我们不在AndroidManifest设置windowSfotinputModle的值,默认就使用这个值,如果在App的主题(theme)中设置了属性值,那么系统就会采用主题里的值,否则系统会根据不同的场景决定键盘的可见性。例如 Activity1中有一个EditText 进入此页面不会显示键盘,但是当使用ScrollView 包裹后,进入此页面就会显示键盘了
stateUnchanged下一个界面的键盘显示状态与当前页面保持一致。 例如Activity2设置了此属性,现在从Activity1导航到Activity2,如果此时键盘是显示的,那么导航Activity2时也是显示的,反之亦然
stateHidden当一个Activity 被设置为此属性时,我们直接打开此界面键盘是隐藏的,但是从其他界面通过Activity栈返回的就不一定了。例如Activity1设置了此属性,现在从Activity1导航到了Activity2,然后在Activity2中调出键盘,然后finish Activity2 回到Activity1,此时键盘就是显示的了
stateAlwaysHidden无论何种方式进入此页面时,键盘都是隐藏的
stateVisible和stateHidden类似,直接打开此页面键盘是显示的,但是从 其他界面通过Activity栈返回的就不一定了。例如Activity1设置了此属性,现在从Activity1导航到了Activity2,然后在Activity2中隐藏键盘,然后finish Activity2 回到Activity1,此时键盘就是隐藏的了
stateAlwaysVisiblestateAlwaysHidden类似,无论以何种方式进入此页面,键盘都是显示的

stateUnspecified

使用ScrollView包裹EditText系统默认会调出键盘

    <application
        ...>
        <activity android:name=".MainActivity" >
            ...
        </activity>
        ...
    </application>

在这里插入图片描述

stateUnchanged

    <application
        ...>
        <activity android:name=".MainActivity"
            android:windowSoftInputMode="stateUnspecified">
            ...
        </activity>
        <activity android:name=".Activity1" android:windowSoftInputMode="stateUnchanged" />
        ...
    </application>

在这里插入图片描述

stateHidden

    <application
        ...>
        <activity android:name=".MainActivity"
            android:windowSoftInputMode="stateHidden">
            ...
        </activity>
        <activity android:name=".Activity1" android:windowSoftInputMode="stateVisible" />
    </application>

在这里插入图片描述

stateAlwaysHidden

    <application
        ...>
        <activity android:name=".MainActivity"
            android:windowSoftInputMode="stateAlwaysHidden">
            ...
        </activity>
        <activity android:name=".Activity1" android:windowSoftInputMode="stateVisible" />
    </application>

在这里插入图片描述

stateVisible

与stateHind 相反

stateAlwaysVisible

与stateAlwaysHind 相反

软键盘与Window的布局关系

属性值含义
adjustResize整个window 从底部整体抬升键盘的高度,顶部不会被顶上去,中间区域被下面顶上的内容覆盖。这个效果和你的布局方式有非常大的关系,在一些布局方式中,是没有效果的。
adjustPan键盘会顶到输入焦点下面,顶部都会被顶上去,输入控件下面的内容会被覆盖
adjustUnspecified系统的默认设置,系统会根据布局中是否有滚动布局来觉得采用哪种方式,有滚动布局:adjustResize 没有滚动布局adjustPan
adjustNothing布局不发生任何变化,键盘覆盖在布局上面

adjustResize

注意:其行为与布局方式相关,所以如果你发现这个属性怎么不起作用的时候,就回头好好看看自己的布局方式。
例如对于如下布局的行为见下面的gif图

布局1

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity2">
    <TextView
        android:id="@+id/tv_des"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个Activity"
        android:textColor="@color/colorAccent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="25dp"/>
    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/tv_declare"
        android:layout_marginBottom="25dp"
        android:hint="输入"/>
    <TextView
        android:id="@+id/tv_declare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="150dp"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="15sp"
        android:text="ShuSheng007出品,必属精品"/>

</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述
注意我们的EditText是以底部为基准布局的,其上面有可以覆盖的空间,没有定死。

我们再看一种布局,这种布局EditView 直接使用android:layout_marginTop="500dp"定死了,那这个属性就不起作用了

布局2

androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity2">
    <TextView
        android:id="@+id/tv_des"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个Activity"
        android:textColor="@color/colorAccent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="25dp"/>
    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_des"
        android:layout_marginTop="500dp"
        android:hint="输入"/>
    <TextView
        android:id="@+id/tv_declare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et_input"
        android:textColor="@color/colorPrimaryDark"
        android:layout_marginTop="15dp"
        android:textSize="15sp"
        android:text="ShuSheng007出品,必属精品"/>

</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述
布局3在布局2的基础上里面加了ScrolView空间

布局3

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity2">
    <TextView
        android:id="@+id/tv_des"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个Activity"
        android:textColor="@color/colorAccent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="25dp"/>
    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_des"
        app:layout_constraintBottom_toBottomOf="parent">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/et_input"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="500dp"
                android:hint="输入"/>
            <TextView
                android:id="@+id/tv_declare"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/et_input"
                android:layout_marginTop="15dp"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="15sp"
                android:text="ShuSheng007出品,必属精品"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述

adjustPan

对于布局1 2 3 都起作用,且效果一致,真的很专一啊!
在这里插入图片描述

adjustNothing

没什么好演示的,弹出的软键盘直接覆盖在了window上。

如何使用

一个页面的键盘可见性与布局关系可以使用 | 来组合使用,如下所示。
<activity android:name=".Activity1" android:windowSoftInputMode="stateVisible|adjustResize" />

使用建议

一般情况下使用默认方式即可,特别是软键盘布局那块,只通过调整我们自己的布局来实现想要的效果。如果你的顶部栏被顶上去了,就好怀疑你的window是事实上使用了adjustPan方式。

总结

个人觉得这个可以关注了,虽然不难,但是真的是有用,遇到相关问题后可以回来查看一下,正所谓点点关注不迷路,天天都有小惊喜。

最后说一句,我想出去遛一遛,在家半个月了,憋死啦。。。

哪里可以找到我?
Gitbub
个人博客

  • 11
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: android:windowsoftinputmodeAndroid中的一个属性,用于控制软键盘的显示和隐藏。它可以设置为多种模式,如adjustResizeadjustPan等,用于适应不同的界面布局和输入场景。其中,adjustResize模式会自动调整界面布局,以避免软键盘遮挡输入框;adjustPan模式则会将整个界面上移,以保证输入框不被遮挡。这个属性Android应用开发中非常常用,可以提高用户体验和界面交互的效率。 ### 回答2: android:windowSoftInputModeAndroid 系统中一个用于控制软键盘与屏幕界面交互的属性,可以通过在 AndroidManifest.xml 文件中为特定的 Activity 指定这一属性值来实现对软键盘的控制。这一属性值会影响软键盘何时弹出和收起,以及弹出后对界面的遮挡方式等行为。 该属性值的取值范围包括以下几种: - stateUnspecified:使用系统默认行为。这是 Android 系统默认的软键盘控制模式。 - stateUnchanged:不对界面做出任何调整,软键盘出现时不做遮盖处理。 - stateHidden:软键盘在界面中不会弹出,会被隐藏。 - stateAlwaysHidden:软键盘绝对不会弹出,即使用户主动召唤。 - stateVisible:软键盘在界面中出现时不会遮盖输入框等元素。 - stateAlwaysVisible:软键盘会一直出现在屏幕上,无论输入焦点在何处。 除了上述最常见的取值之外,还有其他 6 种组合属性值,包括 adjustResizeadjustPanadjustUnspecified、stateUnchanged|adjustResize、stateHidden|adjustResize、stateVisible|adjustPan 等,它们分别表示针对屏幕调整的方式。例如,stateHidden|adjustResize 表示在软键盘弹出时,界面会进行调整,但不会被软键盘遮盖。 使用 android:windowSoftInputMode 属性可以让开发者更好地控制软键盘与界面的交互效果,从而提升应用程序体验。同时在实际使用中,应该选择合适的值,根据不同的需求和设计要求进行配置。 ### 回答3: android:windowsoftinputmode是一种用于设置Android界面在软键盘弹出时的行为的属性。它可以用来指定如何调整界面布局,使其不受弹出软键盘的影响。 Android:windowsoftinputmode的值有以下几种: 1. adjustResize:自动调整布局大小,以便在软键盘弹出时保持界面的稳定性。这种模式会调整布局,使其不被软键盘遮挡,从而避免了用户无法看到完整界面的问题。 2. adjustPan:将整个界面向上移动,直到软键盘完全弹出。这种模式可以确保输入框在软键盘上方,避免遮挡输入框的问题。 3. adjustNothing:不做任何调整,软键盘会覆盖在界面上。这种模式通常用于自定义控件或者实现手动调整界面布局的情况。 Android:windowsoftinputmode可以在AndroidManifest.xml文件中设置,也可以在代码中动态设置。在使用时应根据实际情况选择合适的模式,保障用户体验的同时确保功能的完整性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ShuSheng007

亲爱的猿猿,难道你又要白嫖?

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

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

打赏作者

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

抵扣说明:

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

余额充值