美文网首页
触摸空白区域隐藏软键盘

触摸空白区域隐藏软键盘

作者: 爱我O就直说 | 来源:发表于2020-03-31 17:05 被阅读0次

    2020-03-31

    首先用了 这个第三方工具库: api 'com.blankj:utilcode:1.13.11'

    然后在Activity页面里:

    
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                View v = getCurrentFocus();
                if (isShouldHideKeyboard(v, ev)) {
                    KeyboardUtils.hideSoftInput(this);
                    if (KeyboardUtils.isSoftInputVisible(SearchActivity.this)) {
                        return true;
                    }
                }
            }
            return super.dispatchTouchEvent(ev);
        }
    
        // Return whether touch the view.
        private boolean isShouldHideKeyboard(View v, MotionEvent event) {
            if ((v instanceof EditText)) {
                int[] l = {0, 0};
                v.getLocationOnScreen(l);
                int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left + v.getWidth();
                return !(event.getRawX() > left && event.getRawX() < right && event.getRawY() > top && event.getRawY() < bottom);
            }
            return false;
        }
    
    

    相关文章

      网友评论

          本文标题:触摸空白区域隐藏软键盘

          本文链接:https://www.haomeiwen.com/subject/wdqguhtx.html