Android(安卓)开发——浅谈onInterceptTouchEvent、onTouchEvent与onTouch
16lz
2021-01-26
一、onTouch
onTouch是View 中OnTouchListener接口中的方法,处理View及其子类被touch是的事件处理。当然,前提是touch时间能够传递到指定的view。Q1:为什么会传递不到呢?
1: /**
2: * Interface definition for a callback to be invoked when a touch event is
3: * dispatched to this view. The callback will be invoked before the touch
4: * event is given to the view.
5: */
6: public interface OnTouchListener {
7: /**
8: * Called when a touch event is dispatched to a view. This allows listeners to
9: * get a chance to respond before the target view.
10: *
11: * @param v The view the touch event has been dispatched to.
12: * @param event The MotionEvent object containing full information about
13: * the event.
14: * @return True if the listener has consumed the event, false otherwise.
15: */
16: boolean onTouch(View v, MotionEvent event);
17: }
二、onTouchEvent
onTouchEvent同样也是在view中定义的一个方法。处理传递到view 的手势事件。手势事件类型包括ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL四种事件。
1: /**
2: * Implement this method to handle touch screen motion events.
3: *
4: * @param event The motion event.
5: * @return True if the event was handled, false otherwise.
6: */
7: public boolean onTouchEvent(MotionEvent event) {
8: ……
9: ……
10: }
一旦onTouchEvent方法被调用,并返回true则这个手势事件就结束了,并不会向下传递到子控件。Q2:onTouchEvent 什么时候被调用呢?
三、onInterceptTouchEvent
onInterceptTouchEvent是在ViewGroup里面定义的。Android中的layout布局类一般都是继承此类的。 onInterceptTouchEvent是用于拦截手势事件的,每个手势事件都会先调用onInterceptTouchEvent。
1: public boolean onInterceptTouchEvent(MotionEvent ev) {
2: return false;
3: }
此方法返回false,则手势事件会向子控件传递;返回true,则调用 onTouchEvent方法。
onTouch是View 中OnTouchListener接口中的方法,处理View及其子类被touch是的事件处理。当然,前提是touch时间能够传递到指定的view。Q1:为什么会传递不到呢?
1: /**
2: * Interface definition for a callback to be invoked when a touch event is
3: * dispatched to this view. The callback will be invoked before the touch
4: * event is given to the view.
5: */
6: public interface OnTouchListener {
7: /**
8: * Called when a touch event is dispatched to a view. This allows listeners to
9: * get a chance to respond before the target view.
10: *
11: * @param v The view the touch event has been dispatched to.
12: * @param event The MotionEvent object containing full information about
13: * the event.
14: * @return True if the listener has consumed the event, false otherwise.
15: */
16: boolean onTouch(View v, MotionEvent event);
17: }
二、onTouchEvent
onTouchEvent同样也是在view中定义的一个方法。处理传递到view 的手势事件。手势事件类型包括ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL四种事件。
1: /**
2: * Implement this method to handle touch screen motion events.
3: *
4: * @param event The motion event.
5: * @return True if the event was handled, false otherwise.
6: */
7: public boolean onTouchEvent(MotionEvent event) {
8: ……
9: ……
10: }
一旦onTouchEvent方法被调用,并返回true则这个手势事件就结束了,并不会向下传递到子控件。Q2:onTouchEvent 什么时候被调用呢?
三、onInterceptTouchEvent
onInterceptTouchEvent是在ViewGroup里面定义的。Android中的layout布局类一般都是继承此类的。 onInterceptTouchEvent是用于拦截手势事件的,每个手势事件都会先调用onInterceptTouchEvent。
1: public boolean onInterceptTouchEvent(MotionEvent ev) {
2: return false;
3: }
此方法返回false,则手势事件会向子控件传递;返回true,则调用 onTouchEvent方法。
更多相关文章
- Android——自定义AlertDialog
- ViewPager + Fragment 替换 TabActivity
- 疯狂Android讲义--- 一些有手写识别意思的小玩意儿
- IBinder对象在进程间传递的形式(二)
- Android(安卓)ListView中onItemClickListener事件失效的解决方法
- android GridView 长按事件不管用
- Android(安卓)2.3 r1 中文 API (57) —— ScaleGestureDetector
- Android(安卓)动态添加按钮,并获取id,添加事件
- Android:Click,LongClick,Touch,KeyDown,KeyUp事件使用心得