《每周一记》之Android软键盘

为之于未有,治之于未乱。

在以往的项目开发中,关于软键盘的知识点一直比较模糊,只是知道简单的使用,当遇到问题的时候,也只能靠度娘或者蒙,刚好最近一个同事询问相关问题时,才发现自己知识的薄弱,正好以此来激励自己不断学习。这也正是这边文章的诞生的原因,更是让我决定开始记录《每周一记》。

参考:官方文档

一、WindowSoftInputMode属性

活动的主窗口如何与包含屏幕上的软键盘窗口交互,这个属性的设置将会影响两件事情:

  • 软键盘的状态:当活动(Activity)成为用户关注的焦点时,它是否隐藏或显示。
  • 对活动主窗口进行的调整:无论是调整大小以便为软键盘腾出空间,还是在软键盘覆盖窗口的一部分,以便当前焦点内容可见。

How the main window of the activity interacts with the window containing the on-screen soft keyboard. The setting for this attribute affects two things:

  • 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.
1、属性详解

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:

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

此处设置的值(“stateUnspecified”和“adjustUnspecified”除外)将覆盖主题中设置的值。

Values set here (other than "stateUnspecified" and "adjustUnspecified") override values set in the theme.

用来设置窗口软键盘的交互模式,其属性一共有9个取值,分别是:stateUnspecified,stateUnchanged,stateHidden,stateAlwaysHidden,stateVisible,stateAlwaysVisible,adjustUnspecified,adjustResize,adjustPan。

  • stateUnspecified
    默认交互方式,系统会根据界面采取相应的软键盘的显示模式。比如,当界面上只有输入框和按钮的时候,软键盘就不会自动弹出,但是当有获得焦点的输入框的界面有滚动的需求的时候,会自动弹出软键盘,例如外层为ScrollView。阻止键盘弹出的一个解决方案就是,在xml文件中,设置一个非输入框控件获取焦点。
    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
    当用户导航到而不是返回到该activity时,软键盘总是被隐藏。
    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
    当Activity的主窗口拥有输入焦点时,软键盘总是被隐藏。
    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.

  • adjustUnspecified
    设置软键盘与软件的显示内容之间的显示关系,默认的设置模式。在这中情况下,系统会根据界面选择不同的模式。如果界面里面有可以滚动的控件,比如ScrowView,系统会减小可以滚动的界面的大小,从而保证即使软键盘显示出来了,也能够看到所有的内容。如果布局里面没有滚动的控件,那么软键盘可能就会盖住一些内容。没有滚动控件,软键盘下面的布局都被遮挡住,若想修改,只能隐藏软键盘,然后选择。而且,重点注意一下上面的布局,当我们选择的输入框偏下的时候,上面的标题栏和布局被软键盘顶上去了。
    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.

  • adjustResize(压缩模式)
    这个属性表示Activity的主窗口总是会被调整大小,从而保证软键盘显示空间。
    The activity's main window is always resized to make room for the soft keyboard on screen.

  • adjustPan(平移模式)
    如果设置为这个属性,那么Activity的屏幕大小并不会调整来保证软键盘的空间,而是采取了另外一种策略,系统会通过布局的移动,来保证用户要进行输入的输入框肯定在用户的视野范围内,从而让用户可以看到自己输入的内容。对于没有滚动控件的布局来说,这个其实就是默认的设置,如果我们选择的位置偏下,上面的标题栏和部分控件会被顶上去。
    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.

备注:如果我们不设置"adjust..."的属性,对于没有滚动控件的布局来说,采用的是adjustPan方式,而对于有滚动控件的布局,则是采用的adjustResize方式。

2、使用方式
  • 代码实现方式:
//在activity中的setContentView之前写上以下代码
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
  • xml实现方式:
//在 项目的AndroidManifest.xml文件中界面对应的<activity>里加入
android:windowSoftInputMode="adjustPan"

二、手动打开、关闭软键盘

在开发过程中,难免会遇到想手动打开或者关闭软键盘的情况,这时使用以下代码不失为一种好办法。

/**
 * 动态显示隐藏软键盘
 *
 * @param context context
 */
public static void toggleSoftInput(Context context) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}

三、软键盘的Enter键

1、使用方式
  • 当layout中有多个EditText,把每个控件的android:singleLine的属性都被设置成true的情况下,软键盘的Enter键上 的文字会变成“Next”,按下后下个EditText会自动获得焦点(实现了“Next”的功能);当最后一个控件获得焦点的时候,Enter键上的文 字会变成“Done”,按下后软键盘会自动隐藏起来。

  • 把EditText的ImeOptions属性设置成不同的值,Enter键上可以显示不同的文字或图案
    actionNone : 回车键,按下后光标到下一行
    actionGo : Go,
    actionSearch : 一个放大镜
    actionSend : Send
    actionNext : Next
    actionDone : Done,隐藏软键盘,即使不是最后一个文本输入框

inputView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                     //do something;              
                    return true;
                }
                return false;
            }
        });

推荐阅读更多精彩内容