Get back key event on EditText

前端 未结 4 1098
再見小時候
再見小時候 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:50

    Thank you Reno. It probably seems to work, but I managed to solve it differently.

    I overrode EditText's onKeyPreIme(int keyCode, KeyEvent event). This method intercepts keypresses on the IME. =D

    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && 
            event.getAction() == KeyEvent.ACTION_UP) {
                // do your stuff
                return false;
        }
        return super.dispatchKeyEvent(event);
    }
    
    0 讨论(0)
  • 2020-12-02 17:50

    This does not work ?

    edText.setOnKeyListener(new OnKeyListener()
        {
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if (event.getAction() == KeyEvent.ACTION_DOWN)
                {
                    //check if the right key was pressed
                    if (keyCode == KeyEvent.KEYCODE_BACK)
                    {
    
                        return true;
                    }
                }
                return false;
            }
        });
    

    EDIT :

    Alright this is depressing. Android does not send IME events on closure of the qwerty keypad. This is the only workaround that ive come across. I hope it works for you as well.

    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
  • 2020-12-02 18:14

    Non of the other answers were working for me in SearchView, I've finally end up with overriding dispatchKeyEventPreIme(...) method in my custom view:

    class ImeAwareSearchView @JvmOverloads constructor(
        context: Context?,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0
    ) : SearchView(context, attrs, defStyleAttr) {
    
        var onKeyEventPreImeListener: OnKeyEventPreImeListener? = null
    
        override fun dispatchKeyEventPreIme(event: KeyEvent?): Boolean {
            onKeyEventPreImeListener?.onPreImeKeyEvent()
            return false
        }
    }
    

    The listener looks like this:

    interface OnKeyEventPreImeListener {
    
        fun onPreImeKeyEvent()
    }
    

    And I'm setting it in Fragment to hide my search row:

    search_input.onKeyEventPreImeListener = object: OnKeyEventPreImeListener {
        override fun onPreImeKeyEvent() {
            hideSearchRow()
        }
    }
    

    Note that dispatchKeyEventPreIme(...) method is called twice, so make sure you don't do your staff on event twice as well.

    0 讨论(0)
提交回复
热议问题