我试图在我的活动中设置图像按钮(pushClick),以启用用于旋转针形图形的onTouchEvents。不幸的是,无论我是否单击图像按钮,onTouchEvent均处于活动状态。如何防止在单击图像按钮之后触发onTouchEvent?

public void pushClick(View pushClick) {
    switch (pushClick.getId()) {
    case R.id.btn_push:

        make(degrees);

    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        startRotating();
        break;
    case MotionEvent.ACTION_UP:
        stopRotating();
        break;
    }

    return super.onTouchEvent(event);
}

private void startRotating() {
    returnRotating = false;

    if (!keepRotating) {
        keepRotating = true;

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (keepRotating) {
                    degrees = (degrees + 1) % 360;
                    make(degrees);

                    handler.postDelayed(this, INTERVAL);
                }
            }
        }, INTERVAL);
    }
}

private void stopRotating() {
    keepRotating = false;

    if (!returnRotating) {
        returnRotating = true;

        final Handler handler = new Handler();

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (returnRotating) {
                    degrees = (degrees - 1) % 360;
                    make(degrees);

                    handler.postDelayed(this, INTERVAL);
                }
            }
        }, INTERVAL);
    }
}

最佳答案

我敢打赌,有一种更好的方法可以做到这一点,但是这是我的头上问题:

private boolean buttonClicked = false;

public void pushClick(View pushClick) {
    switch (pushClick.getId()) {
    case R.id.btn_push:
        buttonClicked = true;
        make(degrees);

    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(buttonClicked)
    {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            startRotating();
            break;
        case MotionEvent.ACTION_UP:
            stopRotating();
            break;
        }
    }

    return super.onTouchEvent(event);
}

关于java - 在onClick事件之后启动onTouchEvent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10646214/

10-08 23:22