Contextual ActionBar hides when I click on hardware backbutton and the keyboard is out

久未见 提交于 2019-12-11 11:16:48

问题


I am using the Contextual ActionBar for editing and when I have the keyboard shown and I want to hide it by pressing the hardware back button, it hides the keyboard but it also cancels the contextual actionbar and I really can´t find a way how to keep it on.

Anyone?


回答1:


You should try to override the Back Key hardware, and handle the expected behaviour with a boolean as follows:

// 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;
}  

Another solution might be to call dispatchKeyEvent method which is still called when the CAB is displayed:

@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);
}  

This should do the trick, but I have not tested it. Hope this helps.
Sources: How to Override android's back key when softkeyboard is open - Prevent to cancel Action Mode by press back button



来源:https://stackoverflow.com/questions/23121128/contextual-actionbar-hides-when-i-click-on-hardware-backbutton-and-the-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!