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

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

    I got this working with a slight variant on Fernando Camarago's solution. In my onCreate method I attach a single onTouchListener to the root view but send the view rather than activity as an argument.

            findViewById(android.R.id.content).setOnTouchListener(new OnTouchListener() {           
            public boolean onTouch(View v, MotionEvent event) {
                Utils.hideSoftKeyboard(v);
                return false;
            }
        });
    

    In a separate Utils class is...

        public static void hideSoftKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    

提交回复
热议问题