点击屏幕上EditText区域以外的任何地方隐藏键盘的解决方法

解决思路与iOS中的事件分发机制是类似的,这是Activity.class中的事件分发函数:

(1)下面的函数可以处理所有的点击事件,但是要注意到不能无故拦截。

    /**
     * Called to process touch screen events.  You can override this to
     * intercept all touch screen events before they are dispatched to the
     * window.  Be sure to call this implementation for touch screen events
     * that should be handled normally.
     * 
     * @param ev The touch screen event.
     * 
     * @return boolean Return true if this event was consumed.
     */
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            onUserInteraction();
        }
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

(2)用户布局文件中定义的元素都可以接受TouchEvent,下面的函数只是处理窗口之外空白区域的点击。

    /**
     * Called when a touch screen event was not handled by any of the views
     * under it.  This is most useful to process touch events that happen
     * outside of your window bounds, where there is no view to receive it.
     * 
     * @param event The touch screen event being processed.
     * 
     * @return Return true if you have consumed the event, false if you haven't.
     * The default implementation always returns false.
     */
    public boolean onTouchEvent(MotionEvent event) {
        if (mWindow.shouldCloseOnTouch(this, event)) {
            finish();
            return true;
        }
        
        return false;
    }

因此,需要拦截点击事件进行重新分发,在Activity的任意子类(含SDK或者用户自定义的)中都可以重写:

    //点击EditText以外的任何区域隐藏键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {  
            View v = getCurrentFocus();
            if (Utils.isShouldHideInput(v, ev)) {
                if(Utils.hideInputMethod(this, v)) {
                    return true; //隐藏键盘时,其他控件不响应点击事件==》注释则不拦截点击事件
                }
            }
        }
        return super.dispatchTouchEvent(ev);   
    }
    public static boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            v.getLocationInWindow(l);
            int left = leftTop[0],  top = leftTop[1],  bottom = top + v.getHeight(),  right = left
                    + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    public static Boolean hideInputMethod(Context context, View v) {
	InputMethodManager imm = (InputMethodManager) context
		.getSystemService(Context.INPUT_METHOD_SERVICE);
	if (imm != null) {
		return imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}
	return false;
    }


猜你喜欢

转载自blog.csdn.net/xujunfeng000/article/details/9040641