android softkeyboard showSoftInput vs toggleSoftInput

前端 未结 8 961
一向
一向 2020-12-29 20:47

showSoftInput() doesn\'t show the keyboard for me, but toggleSoftInput() does. I saw some other post that said to disable the hard keyboard when us

8条回答
  •  萌比男神i
    2020-12-29 21:41

    ShowSoftInput works if the imm's target view is same with the editText. You can check this by imm.isActive(editText) or editText.isInputMethodTarget().

    ToggleSoftInput is just toggling the keyboard of the current target of imm.

    Target view of imm is set after the focus has been changed by editText.requestFocus(). I think some post processes exist between these two tasks since one post runnable was not enough. I tested double post and it worked for me.

    editText.requestFocus();
    editText.post(new Runnable() {
        @Override
        public void run() {
            editText.post(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.showSoftInput(editText, 0);
                    }
                }
            });
        }
    });
    

提交回复
热议问题