码迷,mamicode.com
首页 > 移动开发 > 详细

Android事件分发详解(六)——ACTION_DOWN的消费验证

时间:2015-01-05 10:54:33      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

MainActivity如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cn.c;
import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
 
/**
 * Demo描述:
 * 分析Android事件分发和处理机制
 *
 * 在该示例中涉及到三个自定义的View.分别是:
 * 最外层的布局MyFrameLayout
 * 内层的布局MyLinearLayout
 * 最里层的自定义按钮MyButton
 *
 * 在dispatchTouchEvent()源码分析中提到一个很重要的东西:
 * 如果一个View没有处理ACTION_DOWN事件,即对于该事件返回了false(没有消费该事件)
 * 那么后续的ACTION_MOVE和ACTION_UP均不会再传递到该View;也就是说该View没有了
 * 处理ACTION_MOVE和ACTION_UP的资格.
 * 对于该问题,在此予以验证.
 *
 *
 * MyButton的onTouchEvent()方法中直接返回false.
 * 那么可以看到MyButton只处理了ACTION_DOWN.
 * 类似的MyFrameLayout和MyLinearLayout对于Touch事件也直接返回了false;他们也就处理不到
 * ACTION_MOVE和ACTION_UP
 *
 */
 
public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.println(===> MainActivity 中调用 onCreate());
        System.out.println(--------------------------------------------------);
    }
     
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        System.out.println(===> MainActivity 中调用 dispatchTouchEvent());
        System.out.println(===> super.dispatchTouchEvent()默认返回true);
        System.out.println(--------------------------------------------------);
        return super.dispatchTouchEvent(ev);
    }
 
    @Override
    public void onUserInteraction() {
        System.out.println(===> MainActivity 中调用 onUserInteraction());
        System.out.println(--------------------------------------------------);
        super.onUserInteraction();
    }
     
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            System.out.println(===> MainActivity 中调用  onTouchEvent()--->ACTION_DOWN);
            break;
        case MotionEvent.ACTION_MOVE:
            System.out.println(===> MainActivity 中调用  onTouchEvent()--->ACTION_MOVE);
            break;
        case MotionEvent.ACTION_UP:
            System.out.println(===> MainActivity 中调用  onTouchEvent()--->ACTION_UP);
        default:
            break;
        }
        System.out.println(super.onTouchEvent()默认返回false 表示未消费事件);
        System.out.println(--------------------------------------------------);
        return super.onTouchEvent(event);
    }
     
}


MyFrameLayout如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cn.c;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;
 
public class MyFrameLayout extends FrameLayout{
    public MyFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        super.dispatchTouchEvent(ev);
        System.out.println(外层MyFrameLayout 中调用  dispatchTouchEvent());
        System.out.println(super.dispatchTouchEvent()默认返回true 表示继续分发);
        System.out.println(--------------------------------------------------);
        return super.dispatchTouchEvent(ev);
        //return false;
    }
     
    //覆写自ViewGroup
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        System.out.println(外层MyFrameLayout 中调用  onInterceptTouchEvent());
        System.out.println(super.onInterceptTouchEvent()默认返回false 表示不拦截);
        System.out.println(--------------------------------------------------);
        return super.onInterceptTouchEvent(ev);
    }
    //注意:
    //1 ViewGroup是View的子类
    //2 ViewGroup中onTouchEvent()方法默认返回的是false
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            System.out.println(外层MyFrameLayout 中调用  onTouchEvent()--->ACTION_DOWN);
            break;
        case MotionEvent.ACTION_MOVE:
            System.out.println(外层MyFrameLayout 中调用  onTouchEvent()--->ACTION_MOVE);
            break;
        case MotionEvent.ACTION_UP:
            System.out.println(外层MyFrameLayout 中调用  onTouchEvent()--->ACTION_UP);
        default:
            break;
        }
        System.out.println(super.onTouchEvent()默认返回false 表示未消费事件);
        System.out.println(--------------------------------------------------);
        return super.onTouchEvent(event);
        //return true;
    }
     
     
 
}

 

 

MyLinearLayout如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cn.c;
 
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;
 
public class MyLinearLayout extends LinearLayout {
    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
     
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        super.dispatchTouchEvent(ev);
        System.out.println(内层MyLinearLayout 中调用  dispatchTouchEvent());
        System.out.println(super.dispatchTouchEvent()默认返回true 表示继续分发);
        System.out.println(--------------------------------------------------);
        return super.dispatchTouchEvent(ev);
        //return false;
    }
     
    //覆写自ViewGroup
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        super.onInterceptTouchEvent(ev);
        System.out.println(内层MyLinearLayout 中调用  onInterceptTouchEvent());
        System.out.println(super.onInterceptTouchEvent()默认返回false 表示不拦截);
        System.out.println(--------------------------------------------------);
        return super.onInterceptTouchEvent(ev);
         
    }
    //注意:
    //1 ViewGroup是View的子类
    //2 ViewGroup中onTouchEvent()方法默认返回的是false
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            System.out.println(内层MyLinearLayout 中调用  onTouchEvent()--->ACTION_DOWN);
            break;
        case MotionEvent.ACTION_MOVE:
            System.out.println(内层MyLinearLayout 中调用  onTouchEvent()--->ACTION_MOVE);
            break;
        case MotionEvent.ACTION_UP:
            System.out.println(内层MyLinearLayout 中调用  onTouchEvent()--->ACTION_UP);
        default:
            break;
        }
        System.out.println(super.onTouchEvent()默认返回false 表示未消费事件);
        System.out.println(--------------------------------------------------);
        return super.onTouchEvent(event);
    }
     
     
 
}



 

MyButton如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cn.c;
 
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
 
public class MyButton extends Button{
    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
     
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        System.out.println(自定义Button 中调用 dispatchTouchEvent());
        System.out.println(super.dispatchTouchEvent默认返回true);
        System.out.println(--------------------------------------------------);
        return super.dispatchTouchEvent(event);
    }
     
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            System.out.println(自定义Button 中调用 onTouchEvent()--->ACTION_DOWN);
            break;
        case MotionEvent.ACTION_MOVE:
            System.out.println(自定义Button 中调用 onTouchEvent()--->ACTION_MOVE);
            break;
        case MotionEvent.ACTION_UP:
            System.out.println(自定义Button 中调用 onTouchEvent()--->ACTION_UP);
            break;
        default:
            break;
        }
        System.out.println(--------------------------------------------------);
        //return false;
        return true;
    }
     
     
}

 

1
2
3
4
5
<cn.c.myframelayout android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <cn.c.mylinearlayout android:layout_height="wrap_content" android:layout_width="wrap_content">
           <cn.c.mybutton android:layout_height="200dip" android:layout_width="200dip" android:text="自定义Button" android:textcolor="@android:color/black">
    上一篇http://www.2cto.com/kf/201412/365609.html</cn.c.mybutton></cn.c.mylinearlayout>
</cn.c.myframelayout>

 

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

Android事件分发详解(六)——ACTION_DOWN的消费验证

标签:

原文地址:http://www.cnblogs.com/rabbit-bunny/p/4202818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!