android:windowSoftInputMode
Activity 的主窗口与包含屏幕软键盘的窗口之间的交互方式。该属性的设置会影响两点内容:
1.当 Activity 成为用户注意的焦点时,软键盘的状态为隐藏还是可见。
2.对 Activity 主窗口所做的调整 — 是否将其尺寸调小,为软键盘腾出空间;或当软键盘遮盖部分窗口时,是否平移其内容以使当前焦点可见。
值 | 描述 |
---|---|
“stateUnspecified” | 不指定软键盘的状态(隐藏还是可见)。系统会选择合适的状态,或依赖主题中的设置。这是对软键盘行为的默认设置。 |
“stateUnchanged” | 当 Activity 转至前台时保留软键盘最后所处的任何状态,无论是可见还是隐藏。 |
“stateHidden” | 当用户选择 Activity 时(换言之,当用户确实是向前导航到 Activity,而不是因离开另一 Activity 而返回时)隐藏软键盘。 |
“stateAlwaysHidden” | 当 Activity 的主窗口有输入焦点时始终隐藏软键盘。 |
“stateVisible” | 在正常的适宜情况下(当用户向前导航到 Activity 的主窗口时)显示软键盘。 |
“stateAlwaysVisible” | 当用户选择 Activity 时(换言之,当用户确实是向前导航到 Activity,而不是因离开另一 Activity 而返回时)显示软键盘。 |
“adjustUnspecified” | 不指定 Activity 的主窗口是否通过调整尺寸为软键盘腾出空间,或者是否通过平移窗口内容以在屏幕上显示当前焦点。根据窗口的内容是否存在任何可滚动其内容的布局视图,系统会自动选择其中一种模式。如果存在这种视图,系统会调整窗口尺寸,前提是可通过滚动操作在较小区域内看到窗口的所有内容。这是对主窗口行为的默认设置。 |
“adjustResize” | 始终调整 Activity 主窗口的尺寸,以为屏幕上的软键盘腾出空间。 |
“adjustPan” | 不通过调整 Activity 主窗口的尺寸为软键盘腾出空间。相反,窗口的内容会自动平移,使键盘永远无法遮盖当前焦点,以便用户始终能看到自己输入的内容。这通常不如调整窗口尺寸可取,因为用户可能需关闭软键盘才能进入被遮盖的窗口部分,并与之进行交互。 |
public static final int windowSoftInputMode
Defines the default soft input state that this window would like when it is displayed. Corresponds to WindowManager.LayoutParams.softInputMode.
Must be one or more (separated by ‘|’) of the following constant values.
Constant | Value | Description |
---|---|---|
adjustNothing | 30 | Don’t resize or pan the window to make room for the soft input area; the window is never adjusted for it. |
adjustPan | 20 | Don’t resize the window to make room for the soft input area; instead pan the contents of the window as focus moves inside of it so that the user can see what they are typing. This is generally less desireable than panning because the user may need to close the input area to get at and interact with parts of the window. |
adjustResize | 10 | Always resize the window: the content area of the window is reduced to make room for the soft input area. |
adjustUnspecified | 0 | The window resize/pan adjustment has not been specified, the system will automatically select between resize and pan modes, depending on whether the content of the window has any layout views that can scroll their contents. If there is such a view, then the window will be resized, with the assumption being that the resizeable area can be reduced to make room for the input UI. |
stateAlwaysHidden | 3 | Always make the soft input area hidden when this window has input focus. |
stateAlwaysVisible | 5 | Always make the soft input area visible when this window has input focus. |
stateHidden | 2 | Make the soft input area hidden when normally appropriate (when the user is navigating forward to your window). |
stateUnchanged | 1 | Leave the soft input window as-is, in whatever state it last was. |
stateUnspecified | 0 | Not specified, use what the system thinks is best. This is the default. |
stateVisible | 4 | Make the soft input area visible when normally appropriate (when the user is navigating forward to your window). |
参考文章:
https://developer.android.com/guide/topics/manifest/activity-element#wsoft
https://developer.android.com/reference/android/R.attr.html#windowSoftInputMode
在AndroidManifest.xml中修改:
增加android:windowSoftInputMode=“adjustPan”,修改后发现无效果,界面依旧被输入法挤压。
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
增加android:windowSoftInputMode=“adjustNothing”,生效,界面不会被挤压。
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustNothing">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>