1

我正在使用上下文操作栏进行编辑,当我显示键盘并且我想通过按下硬件后退按钮来隐藏它时,它隐藏了键盘,但它也取消了上下文操作栏,我真的找不到方法继续。

任何人?

4

1 回答 1

1

您应该尝试覆盖Back Key硬件,并使用boolean如下处理预期行为:

// boolean isVisible to retrieve the state of the SoftKeyboard
private boolean isVisible = false;

// isVisible becomes 'true' if the user clicks on EditText

// then, if the user press the back key hardware, handle it:
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // check isVisible
        if(isVisible) {
            // hide the keyboard
            InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            isVisible = false;
        } else {
            // remove the CAB
            mActionMode.finish();
        }
    }
    return false;
}  

另一种解决方案可能是调用在显示dispatchKeyEvent时仍会调用的方法CAB

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
        // check CAB active and isVisible softkeyboard
        if(mActionModeIsActive && isVisible) {
            InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            isVisible = false;
            return true;
        // Maybe you might do not call the 'else' condition, anyway..
        } else {
            mActionMode.finish();
            return true;
        }
    }
    return super.dispatchKeyEvent(event);
}  

这应该可以解决问题,但我还没有测试过。希望这可以帮助。
资料来源:如何在软键盘打开时覆盖 android 的后退键-防止通过按后退按钮取消操作模式

于 2014-04-17T01:17:16.460 回答