How to close Android Soft KeyBoard programmatically?

前端 未结 14 1300
别跟我提以往
别跟我提以往 2020-12-02 16:33

I am currently showing softkeyboard using the following code

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
im         


        
相关标签:
14条回答
  • 2020-12-02 17:07

    I have tested and this is working:

    ...
    //to show soft keyboard
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    
    //to hide it, call the method again
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    

    By the way, the second parameter of your code is not right, please have a look at here.

    0 讨论(0)
  • 2020-12-02 17:07

    user942821's answer for hiding it works:

    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    

    But this also works for me to hide it:

    imm.toggleSoftInput(0, 0);
    

    You might also want to try:

    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    

    When using '0' in the first parameter sometimes the keyboard toggles on in the wrong places under weird circumstances that I haven't been able to figure out how to duplicate yet. I'm still testing this last example, but will update when I find out more.

    See toggleSoftInput documentation page for more information.

    0 讨论(0)
  • 2020-12-02 17:10

    Use this working code :

    InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    
    0 讨论(0)
  • 2020-12-02 17:12

    You could also try

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    
    0 讨论(0)
  • 2020-12-02 17:12

    This code hides the keyboard from inside onItemClick of an AutoCompleteTextView

    public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) {
         // whatever your code does
         InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
         imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
    }
    
    0 讨论(0)
  • 2020-12-02 17:16

    This works fine

    InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);
    
    0 讨论(0)
提交回复
热议问题