Android 触摸非EditText区域自动隐藏键盘

主要思路就是重写Activity的dispatchTouchEvent方法

    /**
     * 全局事件分发 实现 触摸非输入框控件 隐藏键盘 stone
     *
     * @param motionEvent
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            // 获得当前得到焦点的View,一般情况下就是EditText(特殊情况就是轨迹球或者实体按键会移动焦点)
            View view = this.getCurrentFocus();
            if (isShouldHideInput(view, motionEvent)) {
               closeKeyboard(view);
            }
        }
        return super.dispatchTouchEvent(motionEvent);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时没必要隐藏 stone
     *
     * @param view
     * @param motionEvent
     * @return
     */
    private boolean isShouldHideInput(View view, MotionEvent motionEvent) {
        if (view != null && (view instanceof EditText)) {
            int[] l = {0, 0};
            view.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + view.getHeight(), right = left
                    + view.getWidth();
            if (motionEvent.getX() > left && motionEvent.getX() < right
                    && motionEvent.getY() > top && motionEvent.getY() < bottom) {
                // 点击EditText的事件,忽略它
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditView上,和用户用轨迹球选择其他的焦点
        return false;
    }

    /**
     * 关闭软键盘 stone
     *
     * @param view 输入框
     */
    private void closeKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


猜你喜欢

转载自blog.csdn.net/u013651026/article/details/78903398
今日推荐