事件 MotionEvent
点击和长按可能会同时发生,需要在长按的回调函数中返回true,就不会产生点击。谁处理事件谁就是消费者

如果view组件不处理事件,最后会让ontouchevent处理,它是备胎


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.atguigu.l06_event.MyImageView
android:id="@+id/iv_touch_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/atguigu"
android:layout_margin="10dp"/>
</LinearLayout>
public class MotionEventTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_touch);
//得到MYimageView
MyImageView iv_touch_test = (MyImageView) findViewById(R.id.iv_touch_test);
//设置touch监听
iv_touch_test.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("TAG", "MYimageView Listener onTouch() "+event.getAction());
return false;
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.i("TAG", "Activity dispatchTouchEvent() "+ev.getAction());
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("TAG", "Activity onTouchEvent() "+event.getAction());
return super.onTouchEvent(event);
}
}
public class MyImageView extends ImageView {
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.e("TAG", "MyImageView()");
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.e("TAG", "MyImageView dispatchTouchEvent() "+event.getAction());
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e("TAG", "MyImageView onTouchEvent() "+event.getAction());
return false;
//return true;
}
}
在屏幕上点击图片之后 执行顺序如下

处理:调用回调方法或监听。调用回调方法或监听返回true的那个才是消费的,如果事件被消费了就不会再向下传递
如下更改,事件到listner就结束了,就不会再继续往下传递
iv_touch_test.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("TAG", "MYimageView Listener onTouch() "+event.getAction());
return true;
}
});

事件 MotionEvent的更多相关文章
- 触摸事件MotionEvent
触摸事件MotionEvent在用户交互中,占着非常重要的地位.首先,来看看MotionEvent中封装的一些常用的事件常量,它定义了触摸事件的不同类型. 1.单点触摸按下动作 public stat ...
- Android触摸事件MotionEvent详解
触摸事件MotionEvent在用户交互中,占着非常重要的地位.首先,来看看MotionEvent中封装的一些常用的事件常量,它定义了触摸事件的不同类型. 1.单点触摸按下动作 public stat ...
- Android 手势&触摸事件 MotionEvent
1.http://blog.csdn.net/omg_2012/article/details/7881443 这篇相当好啊 2.http://blog.csdn.net/android_tutor/ ...
- 札记:android手势识别,MotionEvent
摘要 本文是手势识别输入事件处理的完整学习记录.内容包括输入事件InputEvent响应方式,触摸事件MotionEvent的概念和使用,触摸事件的动作分类.多点触摸.根据案例和API分析了触摸手势T ...
- 简单例子了解View的事件分发
什么是事件分发 我们在写自定义ViewGroup或者自定义View的时候经常要处理用户的点击事件,如果我们的View在最底层,他在很多ViewGroup里面,我们如何让我们的点击事件准确传递到View ...
- Android 坐标系和 MotionEvent 分析、滑动
1.Android坐标系 在Android中,屏幕最左上角的顶点作为Android坐标系的原点,这个点向左是X轴正方向,这个点向下是Y轴正方向. 系统提供了getLocationOnScreen(in ...
- android事件分发介绍
Android事件分发 事件分发3个步骤 dispatchTouchEvent(event)派发 onInterceptTouchEvent(event)拦截 onTouchEvent(eve ...
- 自定义控件(视图)2期笔记10:自定义视图之View事件分发机制("瀑布流"的案例)
1. Touch事件的传递: 图解Touch事件的传递,如下: 当我们点击子View 02内部的Button控件时候,我们就触发了Touch事件. • 这个Touch事件首先传递给了顶级父View ...
- Android View框架总结(七)View事件分发机制
请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52282833 View布局告一段落,从本篇开始View事件相关分析, ...
随机推荐
- react-dnd 拖拽
介绍 React DnD 是一组 React 高阶组件,可以用来帮你构建复杂的拖拽接口,同时解耦你的组件.React DnD 非常适合像 Trello 和 Storify 这样的应用,在不同地方通过拖 ...
- java mail Received fatal alert: handshake_failure java 无法发送邮件问题 java 发送qq邮件(含源码)
java 无法发送邮件问题 java 发送qq邮件 报错:java mail Received fatal alert: handshake_failure (使用ssl) javax.mail.M ...
- 记一次安装centos7及gnome桌面
https://blog.csdn.net/bingbingtea/article/details/79553669
- Codeforces 553D Nudist Beach(二分答案 + BFS)
题目链接 Nudist Beach 来源 Codeforces Round #309 (Div. 1) Problem D 题目大意: 给定一篇森林(共$n$个点),你可以在$n$个点中选择若干个构 ...
- FMDB使用的数据库的三种形式
FMDB使用的数据库的三种形式 FMDB是iOS平台下一款优秀的第三方SQLite数据库框架.它以Objective-C的方式封装了SQLite的C语言API.使用起来,它更加面向对象,避免冗余的 ...
- layDate 日期与时间组件 入门
首先第一步 在官方下载layDate文件.layUI官网:http://layer.layui.com/ https://www.layui.com/laydate/ layDate文件的下载步 ...
- Java随机数技巧-新手篇
package 向家康; import java.util.concurrent.ThreadLocalRandom; public class 练习21 { public static void m ...
- link2005 重复定义错误
造成LNK2005错误主要有以下几种情况: 1.重复定义全局变量. 对于一些初学编程的程序员,有时候会以为需要使用全局变量的地方就可以使用定义申明一下.其实这是错误的,全局变量是针对整个工程的. 正 ...
- @RequestParam,@PathVariable,@ResponseBody,@RequestBody,@ModelAttribute学习
1.@RequestParam使用于参数上,用于将请求参数映射到指定参数变量上 例如: @RequestMapping(value="/hello",method=RequestM ...
- Android 源码编译记录
问题1:Can't locate Switch.pm in @INC (you may need to install the Switch module) (@INC contains: /etc/ ...