Hide Soft Keyboard on Done Keypress in Android?

前端 未结 6 1466
盖世英雄少女心
盖世英雄少女心 2021-02-01 16:26

I\'m struggling with the done button on the soft keyboard. I can\'t get the soft keyboard Done key press to hide the keyboard. From another button, it works perfectly with

6条回答
  •  北海茫月
    2021-02-01 16:50

    SoftKeyboard can be hide by following way

    In Java class we can write following code to hide keyboard when user press done or enter

    etBid.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                        actionId == EditorInfo.IME_ACTION_DONE ||
                        event != null &&
                                event.getAction() == KeyEvent.ACTION_DOWN &&
                                event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
                {
                    if (event == null || !event.isShiftPressed())
                    {
                        // the user is done typing.
                        InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                        return true; // consume.
                    }
                }
                return false; // pass on to other listeners.
            }
    

提交回复
热议问题