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

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

    There is a simpler approach, based on iPhone same issue. Simply override the background's layout on touch event, where the edit text is contained. Just use this code in the activity's OnCreate (login_fondo is the root layout):

        final LinearLayout llLogin = (LinearLayout)findViewById(R.id.login_fondo);
        llLogin.setOnTouchListener(
                new OnTouchListener()
                {
                    @Override
                    public boolean onTouch(View view, MotionEvent ev) {
                        InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(
                                android.content.Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(mActivity.getCurrentFocus().getWindowToken(), 0);
                        return false;
                    }
                });
    

提交回复
热议问题