简体   繁体   中英

Minimize keyboard instead of going back to previous activity on press back

hi I am building keyboard app when i want to minimize the keyboard it go back to previous activity. eg if i am chatting with friends on whatsapp and want to minimize keyboard on pressing back button it will go back to chatlist instead of minimizing. so all I need is when back button is pressed it shall minimize the keyboard instead of going back to previous activity. please help if you can.

public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case 4:
            if (event.getRepeatCount() == 0 && this.mInputView != null && this.mInputView.handleBack()) {
                return PROCESS_HARD_KEYS;
            }
        case 66:
            return false;
        case 67:
            if (this.mComposing.length() > 0) {
                onKey(-5, null);
                return PROCESS_HARD_KEYS;
            }
            break;
        default:
            if (keyCode == 62 && (event.getMetaState() & 2) != 0) {
                InputConnection ic = getCurrentInputConnection();
                if (ic != null) {
                    ic.clearMetaKeyStates(2);
                    keyDownUp(29);
                    keyDownUp(42);
                    keyDownUp(32);
                    keyDownUp(46);
                    keyDownUp(43);
                    keyDownUp(37);
                    keyDownUp(32);
                    return PROCESS_HARD_KEYS;
                }
            }
            if (this.mPredictionOn && translateKeyDown(keyCode, event)) {
                return PROCESS_HARD_KEYS;
            }
    }
    return super.onKeyDown(keyCode, event);
}

Just replace this hope it will work.

 public boolean onKeyDown(int keyCode, KeyEvent event, View view) {if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// hide keyboard here
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);}

switch (keyCode) {
    case 4:
        if (event.getRepeatCount() == 0 && this.mInputView != null && this.mInputView.handleBack()) {
            return PROCESS_HARD_KEYS;
        }
    case 66:
        return false;
    case 67:
        if (this.mComposing.length() > 0) {
            onKey(-5, null);
            return PROCESS_HARD_KEYS;
        }
        break;
    default:
        if (keyCode == 62 && (event.getMetaState() & 2) != 0) {
            InputConnection ic = getCurrentInputConnection();
            if (ic != null) {
                ic.clearMetaKeyStates(2);
                keyDownUp(29);
                keyDownUp(42);
                keyDownUp(32);
                keyDownUp(46);
                keyDownUp(43);
                keyDownUp(37);
                keyDownUp(32);
                return PROCESS_HARD_KEYS;
            }
        }
        if (this.mPredictionOn && translateKeyDown(keyCode, event)) {
            return PROCESS_HARD_KEYS;
        }
}
return super.onKeyDown(keyCode, event);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM