android softkeyboard showSoftInput vs toggleSoftInput

前端 未结 8 850
一向
一向 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:22
    public void hideKeyboard() {
        myTextView.setFocusable(true);
        myTextView.setFocusableInTouchMode(true);
        imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
    }
    

    WORKS

    public void hideKeyboard() {
        imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
    }
    

    DOES NOT WORK

    imm is dealt with earlier as I am using a Fragment so:

    Declare imm in the Fragment

    private InputMethodManager imm;
    

    Then in the fragment add:

    @Override
        public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        imm = (InputMethodManager)
        getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    }
    

    He says after 3 to 4 hours of research and failures !!

    Thanks user_CC ! :-)

    Phil

    0 讨论(0)
  • 2020-12-29 21:26

    Try this:

    public void showTheKeyboard(Context context, EditText editText){
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }
    

    If this doesn't work read the tutorial from here

    0 讨论(0)
  • 2020-12-29 21:28

    I know this post is pretty old, but for those who came for answers from this date and none of the above worked. The code below worked for me on an Activity when an Alert Dialog popped up.

    InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    

    to show keyboard:

    keyboard.toggleSoftInput(editText.getPaintFlags(), 0);
    

    to hide keyboard:

    keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    
    0 讨论(0)
  • 2020-12-29 21:32

    The precise answer to this question why does showSoftInput doesnt work and toggleSoftInput does?

    Is that the View to which you are trying to display the Keyboard doesn't have the focus. So to solve this problem and to use the method showSoftInput you will have to call the following to methods on your view:

      setFocusable(true);
      setFocusableInTouchMode(true); 
    

    Calling the above methods will make sure that when you click on the View retains and captures the focus.

    0 讨论(0)
  • 2020-12-29 21:37

    showSoftInput() does not work when device has a hard (slide-out) keyboard

    Android show softkeyboard with showSoftInput is not working?

    0 讨论(0)
  • 2020-12-29 21:39

    It seems that the keyboard is initially displayed but hidden by something else, because the following works (but is actually a dirty work-around):

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

    And when looking at logcat I suspect the cause behind this message hides the keyboard initially shown:

    Hide Clipboard dialog at Starting input: finished by someone else... !

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