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

前端 未结 30 2125
醉话见心
醉话见心 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条回答
  •  旧时难觅i
    2020-11-22 12:08

    its too simple, just make your recent layout clickable an focusable by this code:

    android:id="@+id/loginParentLayout"
    android:clickable="true"
    android:focusableInTouchMode="true"
    

    and then write a method and an OnClickListner for that layout , so that when the uppermost layout is touched any where it will call a method in which you will write code to dismiss keyboard. following is the code for both; // you have to write this in OnCreate()

     yourLayout.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View view) {
                        hideKeyboard(view);
                    }
                });
    

    method called from listner:-

     public void hideKeyboard(View view) {
         InputMethodManager imm =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    

提交回复
热议问题