【Android】 点击空白处隐藏(收起)键盘

来源:互联网 发布:淘宝自然流量没有了 编辑:程序博客网 时间:2024/06/05 19:32
在activity页面设置点击空白 收起键盘(直接替换类名就好)
    //点击空白处收起键盘    @Override    public boolean onTouchEvent(MotionEvent event) {        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);        if (event.getAction() == MotionEvent.ACTION_DOWN) {                if (UserLoginActivity.this.getCurrentFocus().getWindowToken() != null) {                    imm.hideSoftInputFromWindow(UserLoginActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);                }            }        return super.onTouchEvent(event);    }

在fragment页面设置
    /**     * 点击空白区域隐藏键盘.      */    @Override    public boolean onTouchEvent(MotionEvent event) {        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);        if (event.getAction() == MotionEvent.ACTION_DOWN) {            if (Fragment.this.getCurrentFocus() != null) {                if (UserLoginActivity.this.getCurrentFocus().getWindowToken() != null) {                    imm.hideSoftInputFromWindow(UserLoginActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);                }            }        }        return super.onTouchEvent(event);    }