android – 处理触摸事件 – onInterceptTouchEvent和onTouchEvent

前端之家收集整理的这篇文章主要介绍了android – 处理触摸事件 – onInterceptTouchEvent和onTouchEvent前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望能够在屏幕上的任何位置滑动以调用某个功能.但我也想要能够点击的线性布局中的按钮.如果我在按钮上滑动我想onInterceptTouchEvent拦截对Button的onTouchEvent的调用并执行滑动操作.如果我只是单击一个按钮,我不希望调用onInterceptTouchEvent.相反,我希望调用Button的onTouchEvent并执行Button单击

但是当我尝试实现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;
}

首先错误说明:Game类型的onInterceptTouchEvent(MotionEvent)方法必须覆盖或实现超类型方法

然后,而不是返回true我将代码更改为:return super.onInterceptTouchEvent(event)但后来我得到一个错误说:对于类型Activity,未定义onInterceptTouchEvent(MotionEvent)方法

有人可以帮忙吗?

解决方法

请注意,onInterceptTouchEvent()是ViewGroup类中的方法,而不是来自Activity.

您可以通过将逻辑从onInterceptTouchEvent()移动到dispatchTouchEvent(MotionEvent ev)来实现所需的行为.请记住调用dispatchTouchEvent(MotionEvent ev)的超类实现来处理应该正常处理的事件.

另请注意,只有在delta大于system constant for touch slop时才应考虑移动.我建议用户通过测试yDelta / 2>确保用户正在按照您想要的方向滑动. xDelta而不是yDelta> xDelta.

public class Game extends Activity {
    private int mSlop;
    private float mDownX;
    private float mDownY;
    private boolean mSwiping;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_activity);

        ViewConfiguration vc = ViewConfiguration.get(this)
        mSlop = vc.getScaledTouchSlop();

       //other code....
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownX = ev.getX();
                mDownY = ev.getY();
                mSwiping = false;
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if(mSwiping) {
                    swipeScreen(); //if action recognized as swipe then swipe
                }
                break;
            case MotionEvent.ACTION_MOVE:
                float x = ev.getX();
                float y = ev.getY();
                float xDelta = Math.abs(x - mDownX);
                float yDelta = Math.abs(y - mDownY);

                if (yDelta > mSlop && yDelta / 2 > xDelta) {
                    mSwiping = true;
                    return true;
                }
                break;
        }

        return super.dispatchTouchEvent(ev);
    }
}

猜你在找的Android相关文章