经验谈

hideSoftInputFromWindow方法失效问题解决

2018-03-29  本文已影响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);

解决,记录。

上一篇 下一篇

猜你喜欢

热点阅读