【发布时间】:2015-02-08 22:42:55
【问题描述】:
我希望能够在屏幕上的任意位置滑动来调用某个函数。但我在Linear Layouts 中也有Buttons,我希望能够点击它。如果我在Button 上滑动,我希望onInterceptTouchEvent 到Intercept 调用Button 的onTouchEvent 并执行滑动操作。如果我只是单击Button,我不想调用onInterceptTouchEvent。相反,我希望调用Button 的onTouchEvent 并执行Button click
但是当我尝试实现onInterceptTouchEvent 时出现错误。
这是我的代码:
public class Game extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_activity);
//other code....
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
swipeScreen(); //if action recognized as swipe then swipe
break;
case MotionEvent.ACTION_MOVE:
float x = event.getX();
float y = event.getY();
float xDelta = Math.abs(x - mLastX);
float yDelta = Math.abs(y - mLastY);
if (yDelta > xDelta) {
return true;
}
break;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
ButtonOnClick(); //if not a swipe, then button click
return true;
}
首先错误显示:The method onInterceptTouchEvent(MotionEvent) of type Game must override or implement a supertype method
然后,我将代码更改为:return super.onInterceptTouchEvent(event),而不是 return true,但随后出现错误消息:The method onInterceptTouchEvent(MotionEvent) is undefined for the type Activity
有人可以帮忙吗?
【问题讨论】:
-
onInterceptTouchEvent()方法是ViewGroup类的成员。要实现它,您必须继承ViewGroup以在您的布局中使用。 -
hm,那么我如何子类化我的视图组? - 对不起,我只是 android 的新手
-
感谢我掌握了窍门
标签: android onclick touch touch-event