视图的onTouchListener与onTouchEvent [英] A View's onTouchListener vs onTouchEvent

查看:84
本文介绍了视图的onTouchListener与onTouchEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

视图的onTouchEvent

public class MyCustomView extends View {
    // THIS :
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}

及其onTouchListener:

MyCustomView myView = (MyCustomView) findViewById(R.id.customview);
myView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public void onClick(View arg0) {
        // do something
    }
});

public class MyCustomView extends View {

    public MyCustomView(Context context, AttributeSet attrs) {
        // THIS :
        setOnTouchListener(new View.OnTouchListener() {
            @Override
            public void onClick(View arg0) {
                // do something
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}

如果这两个不同,
我们需要同时实现吗?
首先调用哪个?

If this two is different,
Do we need to implement both ?
Which one is invoked first ?

如果我具有一些滚动和缩放功能,是否应该在onTouchEventonTouchListener内实现它们?

If I have some scrolling and zooming functionality, should I implement them inside onTouchEvent or onTouchListener ?

推荐答案

LeeYiHong的回答是正确的,另一个非常重要的事情是

Answer by LeeYiHong is correct, and the other very important thing is what is written at http://developer.android.com/reference/android/view/View.OnTouchListener.html:

在将触摸事件[i.e. onTouchEvent(MotionEvent)]提供给视图之前,将调用回调[i.e. View.OnTouchListener -> onTouch(View v, MotionEvent event)].

The callback [i.e. View.OnTouchListener -> onTouch(View v, MotionEvent event)] will be invoked before the touch event [i.e. onTouchEvent(MotionEvent)] is given to the view.

这篇关于视图的onTouchListener与onTouchEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆