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

前端 未结 30 2306
醉话见心
醉话见心 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

    I find the accepted answer a bit complicated.

    Here's my solution. Add an OnTouchListener to your main layout, ie.:

    findViewById(R.id.mainLayout).setOnTouchListener(this)
    

    and put the following code in the onTouch method.

    InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    

    This way you don't have to iterate over all views.

提交回复
热议问题