温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android开发中onInterceptTouchEvent、onTouchEvent与onTouch的示例分析

发布时间:2021-12-30 15:45:40 来源:亿速云 阅读:106 作者:小新 栏目:移动开发

这篇文章主要为大家展示了“android开发中onInterceptTouchEvent、onTouchEvent与onTouch的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“android开发中onInterceptTouchEvent、onTouchEvent与onTouch的示例分析”这篇文章吧。

一、onTouch

onTouch是View中OnTouchListener接口中的方法,处理View及其子类被touch是的事件处理。当然,前提是touch时间能够传递到指定的view。Q1:为什么会传递不到呢?

/**         * Interface definition for a callback to be invoked when a touch event is         * dispatched to this view. The callback will be invoked before the touch           * event is given to the view.           */          public interface OnTouchListener {              /**              * Called when a touch event is dispatched to a view. This allows listeners to             * get a chance to respond before the target view.             *             * @param v The view the touch event has been dispatched to.             * @param event The MotionEvent object containing full information about             *        the event.              * @return True if the listener has consumed the event, false otherwise.             */             boolean onTouch(View v, MotionEvent event);        }

二、onTouchEvent

onTouchEvent同样也是在view中定义的一个方法。处理传递到view 的手势事件。手势事件类型包括ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL四种事件。

/**     * Implement this method to handle touch screen motion events.      *      * @param event The motion event.      * @return True if the event was handled, false otherwise.      */     public boolean onTouchEvent(MotionEvent event) {      ……      ……    }

一旦onTouchEvent方法被调用,并返回true则这个手势事件就结束了,并不会向下传递到子控件。Q2:onTouchEvent什么时候被调用呢?

三、onInterceptTouchEvent

onInterceptTouchEvent是在ViewGroup里面定义的。Android中的layout布局类一般都是继承此类的。onInterceptTouchEvent是用于拦截手势事件的,每个手势事件都会先调用onInterceptTouchEvent。

 public boolean onInterceptTouchEvent(MotionEvent ev) {             return false;      }

此方法返回false,则手势事件会向子控件传递;返回true,则调用onTouchEvent方法。

以上是“android开发中onInterceptTouchEvent、onTouchEvent与onTouch的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI