Get back key event on EditText

前端 未结 4 1100
再見小時候
再見小時候 2020-12-02 17:39

How can I handle the event of pressing back key while typing on an EditText? When the virtual keyboard is shown and the user presses back, it gets hidden. I want to handle t

4条回答
  •  旧巷少年郎
    2020-12-02 17:55

    I have no idea why this is the case but OnKeyListener works if you just purely override onKeyPreIme on your custom EditText.

    customEditText.setOnKeyListener((v, keyCode, event) -> {
                if(event.getAction() == KeyEvent.ACTION_DOWN) {
                    switch (keyCode) {
                        case KeyEvent.KEYCODE_BACK:
                            getPresenter().onBackPressed();
                            break;
                    }
                }
                return false;
            }); 
    

    @Override
        public boolean onKeyPreIme(int keyCode, KeyEvent event) {
            return super.dispatchKeyEvent(event);
        }
    

提交回复
热议问题