美文网首页经验谈
hideSoftInputFromWindow方法失效问题解决

hideSoftInputFromWindow方法失效问题解决

作者: 西门小贼 | 来源:发表于2018-03-29 11:47 被阅读0次
问题:

在处理一个事件之后显示一个EditView,此EditView获取焦点并显示软键盘,onStop中调用hideSoftInputFromWindow,软键盘不能自动收起。

EditView获取焦点
mSearchInput.setFocusable(true);
                mSearchInput.setFocusableInTouchMode(true);
                mSearchInput.requestFocus();
                mSearchInput.findFocus();
弹出软键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
        }
隐藏软键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && view != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }

此方法在onStop中调用时失效。

原因:

软键盘显示时参数:

/**
     * Flag for {@link #showSoftInput} to indicate that the user has forced
     * the input method open (such as by long-pressing menu)  so it should
     * not be closed until they explicitly do so.
     */
    public static final int SHOW_FORCED = 0x0002;

非强制不收起。此参数导致hideSoftInputFromWindow失效

修改方式:

将SHOW_FORCED 替换为SHOW_IMPLICIT隐式请求软键盘。

imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);

解决,记录。

相关文章

网友评论

    本文标题:hideSoftInputFromWindow方法失效问题解决

    本文链接:https://www.haomeiwen.com/subject/jhlmcftx.html