视图的onTouchListener与onTouchEvent [英] A View's onTouchListener vs 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 ?
如果我具有一些滚动和缩放功能,是否应该在onTouchEvent
或onTouchListener
内实现它们?
If I have some scrolling and zooming functionality, should I implement them inside onTouchEvent
or onTouchListener
?
推荐答案
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屋!