资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

这篇文章主要为大家展示了“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的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


分享文章:android开发中onInterceptTouchEvent、onTouchEvent与onTouch的示例分析
标题来源:http://cdkjz.cn/article/jhisgo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220