How to hide soft keyboard on android after clicking outside EditText?

前端 未结 30 2153
醉话见心
醉话见心 2020-11-22 11:46

Ok everyone knows that to hide a keyboard you need to implement:

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hi         


        
30条回答
  •  天涯浪人
    2020-11-22 12:24

    This one is the easiest solution for me (and worked out by me).

    This is the method to hide the keyboard.

    public void hideKeyboard(View view){
            if(!(view instanceof EditText)){
                InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
            }
        }
    

    now set onclick attribute of the parent layout of the activity to above method hideKeyboard either from the Design view of your XML file or writing below code in Text view of your XML file.

    android:onClick="hideKeyboard"
    

提交回复
热议问题