清单文件中activity属性windowSoftInputMode研究

一、写在前面的废话:

  今天研究聊天详情页时,发现点击输入框获得焦点后,弹起的软键盘没有盖住EditText,而是将输入框顶了上去。一番搜索,原来是通过在清单文件中给对应activity设置属性windowSoftInputMode做到的。接下来就研究一下这个属性吧。

二、知识点:

​ windowSoftInputMode可以取九个值,分别是:

android:windowSoftInputMode=["stateUnspecified",
                                       "stateUnchanged", "stateHidden",
                                       "stateAlwaysHidden", "stateVisible",
                                       "stateAlwaysVisible", "adjustUnspecified",
                                       "adjustResize", "adjustPan"]

​ 从命名就可以看出,前六个值跟状态有关,后三个值跟布局的调整有关。关于这两类取值,可以看google官方API给出的解释:

The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
The adjustment made to the activity’s main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.

​ 前者是当activity获得用户的焦点时,软键盘是弹出还是隐藏;后者是,当软键盘弹出的时候,当前activity的主窗口是否缩小布局以使软键盘有足够的空间展示,或者使布局里的内容向上滑动以使当前获得焦点的View可见。

The setting must be one of the values listed in the following table, or a combination of one “state...“ value plus one “adjust...“ value. Setting multiple values in either group — multiple “state...“ values, for example — has undefined results. Individual values are separated by a vertical bar (|). For example:

​ 这段话指出,这个属性必须从九个值里取,可以只取一个值,也可以是 state…与adjust的组合,如下:

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

​ 关于九个值各自的介绍,官方也给出了解释:

Value Description
stateUnspcified 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. (这个是默认缺省属性值,显示或者隐藏由系统来确定,或者由当前主题来确定)
stateUnchanged The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore.(经测试,似乎没什么卵用。)
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.
stateAlwaysHidden The soft keyboard is always hidden when the activity’s main window has input focus.
stateVisible The soft keyboard is visible when that’s normally appropriate (when the user is navigating forward to the activity’s main window).
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.(设置了这个值或上一个,通过测试发现,进入activity后,软键盘会自动弹起。而设置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.This is the default setting for the behavior of the main window.(缺省值,到底是缩小布局还是滚动布局中获得焦点控件以上的组件,取决于当前布局中是否有可以滚动的View。)
adjustResize The activity’s main window is always resized to make room for the soft keyboard on screen.(触发View的onSizeChanged方法,布局高度缩小了,给软键盘腾出空间。)
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.

​ 妈蛋的,上面的属性值我挨个试了一遍,但与它给出的结果不一致,我都糊涂了。不过,我发现几个比较靠谱的规律:

  • 用的比较多的是adjustResize和adjustPan,前者是缩小布局得大小,后者是向上移动焦点控件之上的View。
  • 当布局中,焦点组件上方有可滚动的View时,设置为adjustPan,会将当前焦点组件上方的所有View向上移动,以使得焦点组件完全露出,且焦点组件下方的View被软键盘遮盖,这种会导致View被顶出界面;设置为adjustResize,可滚动的View会缩小其高度,给软键盘腾出空间,然后其中焦点组件上方的View会向上滚动,这样可滚动的View上方的View不会滚动,即保持位置不变。
  • 如果焦点组件上方没有可滚动的View,设置为adjustPan,跟上面效果一样;设置为adjustResize,不起作用,焦点组件被盖住了!
0%