android softkeyboard showSoftInput vs toggleSoftInput

前端 未结 8 851
一向
一向 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条回答
  • 2020-12-29 21:41

    Show Keyboard + focus and also if you want to Hide the keyboard

    @Override
    public void onResume () {
        super.onResume();
    
        inputSearch.setFocusableInTouchMode(true);
        inputSearch.requestFocus();
    
        // Show Keyboard
        InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(
                Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(inputSearch, InputMethodManager.SHOW_IMPLICIT);
    }
    

    P.S inputSearch = (EditText) getSherlockActivity().findViewById(R.id.inputSearch);

        // Hide Keyboard
    InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
    
    0 讨论(0)
  • 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);
                    }
                }
            });
        }
    });
    
    0 讨论(0)
提交回复
热议问题