0

我正在尝试在我的 Activity 中设置图像按钮 (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);
    }
}
4

2 回答 2

1

我敢打赌,有更好的方法可以做到这一点,但我想不到:

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);
}
于 2012-05-18T03:22:10.577 回答
0

您还可以在按下图像按钮时添加 OnTouchListener(从您想要附加的任何视图),并在再次单击它时将其删除(如果这是您想要的)。

于 2012-05-18T03:29:04.313 回答