android:windowSoftInputMode属性解析

  大家肯定都碰到过输入法不按自己的预期出现的问题,软键盘出现的时机,出现的位置,这个我们都是可以控制的。通过InputMethodManager可以控制软键盘的显示与隐藏,这个就不多说了,同样在AndroidManifest.xml文件中我们也可以对软键盘进行控制。在节点下有这么一个属性,android:windowSoftInputMode,下面结合API指南来一一分析。
  
  android:windowSoftInputMode,决定了当前activity的window和软键盘如何交互。这个属性主要有两方面的作用,其一,软键盘的状态,显示或者隐藏,activity何时获得用户的焦点等。其二,当前界面的调整,界面缩小使得有足够的空间来显示软键盘还是软键盘覆盖在当前界面上使得用户当前焦点所在的区域可见。
  
  这个属性有一下九种值:

stateUnspecified,stateUnchanged,stateHidden,stateAlwaysHidden,StateVisible,StateAlwaysVisible 
adjustUnspecified,adjustResize,adjustPan

   我们可以以“|”连接两个值,当然必须分别是state开头和adjust开头的,例如如下形式:

<activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >

stateUnspecified

The state of the soft keyboard (whether it is hidden or visible) is not specified. The system will choose an appropriate state or rely on the setting in the theme.
This is the default setting for the behavior of the soft keyboard.

  当我们没有设置android:windowSoftInputMode属性时,默认就是这个值。软键盘的显示或隐藏没有被指定,系统将选择一种合适的状态。那么系统选择是否弹出软键盘的依据是什么呢?下面看个例子。
  

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

            <EditText
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="请输入用户名"
                    android:layout_marginTop="20dp"/>

            <Button
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="确定"
                    android:layout_marginTop="20dp"/>

        </LinearLayout>

  这样一个最简单的布局,输入框底下一个确定按钮,运行起来是这样的效果,这里写图片描述
  
  并不会主动弹出输入法,可见在这样的简单布局下系统并不会认为用户想输入些什么。我们再修改一下布局文件,嵌套一个ScrollView,
  

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

            <EditText
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="请输入用户名"
                    android:layout_marginTop="20dp"/>

            <Button
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="确定"
                    android:layout_marginTop="20dp"/>
        </LinearLayout>

    </ScrollView>

  再次运行,效果如下:
  这里写图片描述

  当EditText处于可滑动的区域的时候,系统会认为此时用户需要进行输入,主动弹出软键盘。由此可见,当属性为stateUnspecified时,输入框的界面可以滑动时会主动调出软键盘。当然只是系统是这么判定的,我们并不需要遵守,毕竟软键盘的显示与否我们是可以通过代码进行控制的。

stateUnchanged

The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore.

  软键盘将保持上一次的状态,当Activity跳转的时候,上一界面软键盘存在就继续显示,不存在就不显示。个人认为这个属性并没有什么实际意义,应该很少碰到保持输入法状态这么一个需求。

stateHidden

The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.

  这个状态下软键盘永远是隐藏的(当然除了点击输入框)。接着上面的例子,嵌套ScrollView之后,设置windowSoftInputMode为stateHidden’,软键盘是不会显示的。 上面when the user…之后的英文什么意思谁能告诉我。。。

stateAlwaysHidden

The soft keyboard is always hidden when the activity's main window has input focus.

  查了很多资料也没有发现stateAlwaysHidden与stateHidden的区别在哪里。。有没有了解的大神。

stateVisible

The soft keyboard is visible when that's normally appropriate (when the user is navigating forward to the activity's main window).

  stateVisible如同集齐七龙珠一般,强制召唤软键盘,即使界面中并没有输入框。

stateAlwaysVisible

The soft keyboard is made visible when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.

  stateAlwaysVisible与stateVisible的区别是很明显的。我们从A跳转到B,第一种情况,A是stateVisible,B是stateHidden,刚进入界面A,软键盘是存在的,跳转到B,软键盘隐藏。当我们再次从B返回A的时候会发现,软键盘依旧是隐藏的。但是当A是stateAlwaysVisible的时候,从B返回到A,A中的软键盘是显示的。这就是他们的区别,stateAlwaysVisible可以保证任何情况下软键盘都是存在的。看到这里,大家肯定联想到stateHidden和stateAlwaysHidden是不是也是这个区别呢?我们来试验一下。A为stateAlwaysHidden,B为stateVisible,当从B返回A的时候,A依旧隐藏软键盘,将A改为stateHidden,结果是一样的,和我们预期的并不一样,所以stateHidden和stateAlwaysHidden不存在这样的区别。

adjustUnspecified

It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the contents of the window pan to make the current focus visible on-screen. The system will automatically select one of these modes depending on whether the content of the window has any layout views that can scroll their contents. If there is such a view, the window will be resized, on the assumption that scrolling can make all of the window's contents visible within a smaller area.

  从这个属性开始将决定软键盘的显示区域情况。软键盘的显示位置一般有两种情况,第一种是,软键盘直接覆盖在当前界面上,但是保证用户当前输入的地方,焦点所在的地方处于屏幕当中。第二种是,系统重新调整界面的大小,使得有足够的空间来显示软键盘,说通俗点,就是软键盘把原来的界面抬上去了。相信大家有时候会遇到软键盘把底部整个Tab顶到上面去了。如果我们没有指定android:windowSoftInputMode的值,和stateUnspecified一样,adjustUnspecified是默认的值。那么系统是如何选定上述两种情况的呢?阅读文档可以发现,如何当前界面中含有内部内容可以滑动的控件,系统就会假设用户想看到当前window的所有内容,所以将界面缩小。反之,则直接将输入法覆盖在界面上,同时保持用户能够看到自己的输入。

adjustResize

The activity's main window is always resized to make room for the soft keyboard on screen.

  界面始终通过重新调整大小留出足够的空间显示软键盘,软键盘会把界面顶上去。

adjustPan

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

  adjustPan,设置了这个值之后,系统不会去重新调整当前界面的大小来适应软键盘。取而代之的是,当前window中的内容会自动平铺,使得当前焦点所在区域不会被软键盘阻挡而且用户可以看见自己正在输入的文字。文档中说这种方式没有adjustResize令人满意,我觉得情况不一样,不能一概而论吧。有的时候把整个界面顶上去还是很令人讨厌的。
  总结这个属性,当我们不设置“adjust…”时,默认是adjustUnspecified,当含有滑动控件时,是adjustResize,否则是adjustPan。具体可以根据我们的需求定义这个属性值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值