zoukankan      html  css  js  c++  java
  • 点击屏幕上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;
        }


  • 相关阅读:
    spring源码解析-ApplicationContext解析
    分布式系统的CAP理论
    Java常用设计模式详解1--单例模式
    mysql全方位知识大盘点
    重磅!微软发布 Visual Studio Online:Web 版 VS Code + 云开发环境
    知否知否,VS Code 不止开源
    webpack静态资源拷贝插件
    webpack 清理旧打包资源插件
    webpack 配置分离css插件
    webpack打包指定HTML的文件并引入指定的chunks
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3122908.html
Copyright © 2011-2022 走看看