我正在使用 AndEngine,并且我的自定义单选按钮有以下代码:
blackRadioNotSelected = new Sprite(0, 0, blackRadio, vertexBufferObjectManager) {
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
if (pSceneTouchEvent.isActionUp()) {
activity.runOnUpdateThread(new Runnable() {
@Override
public void run() {
Log.d(TAG,"touch");
unregisterTouchArea(blackRadio);
detachChild(whiteRadio);
detachChild(blackRadio);
whiteRadio = whiteRadioNotSelected;
blackRadio = blackRadioSelected;
registerTouchArea(whiteRadio);
attachChild(whiteRadio);
attachChild(blackRadio);
}
});
isWhiteSelected = !isWhiteSelected;
}
return false;
}
};
当我触摸单选按钮时,我可以看到“触摸”消息,并且在第二次触摸后它不会显示 - 所以,unregisterTouchArea(blackRadio);
被执行了。但 UI 还是一样:whiteRadio 看起来像是选中的,而 blackRadio - 未选中。会是什么呢?
UPD:我尝试将此代码放入其他方法中,一切正常。这就是它现在的样子:
blackRadioNotSelected = new Sprite(0, 0, blackRadio, vertexBufferObjectManager) {
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
if (pSceneTouchEvent.isActionUp()) {
swapColor();
}
return false;
}
};
和swapColor();
:
private void swapColor() {
if (isWhiteSelected) {
activity.runOnUpdateThread(new Runnable() {
@Override
public void run() {
Log.d(TAG,"blackNotSelected");
unregisterTouchArea(blackRadio);
detachChild(whiteRadio);
detachChild(blackRadio);
whiteRadio = whiteRadioNotSelected;
blackRadio = blackRadioSelected;
registerTouchArea(whiteRadio);
attachChild(whiteRadio);
attachChild(blackRadio);
}
});
} else {
[...]
}
isWhiteSelected = !isWhiteSelected;
}
但我仍然不知道出了什么问题,因为以前的版本在其他项目中工作。有人可以向我解释吗?