【发布时间】:2017-01-13 10:55:51
【问题描述】:
我正在开发一个生成多项选择题的应用程序。当用户选择一个按钮(单选按钮)时,它会在所选答案旁边放置一个X(如果错误)或V(如果正确)的图像。我创建了包含所有按钮的 RadioGroup 并使用了OnCheckedChanged Listener。我跟踪并放置了打印语句以确保它有效并且确实有效。但是,除了第一次用户按下之外,它根本不显示可绘制图像。
换句话说,用户将按下其中一个按钮,将看到反馈(V 或 X),但随后按下不同的按钮并不会看到任何内容,尽管逻辑确实有效(我在 if 中使用了 print 语句)。我使用SetCompoundDrawableWithIntrinsicBounds,它就可以工作。有人知道如何解决这个问题吗?
在过去的 8 个小时里,我一直在努力解决这个问题,我感到非常沮丧。
监听代码:
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//get the index of the correct answer
int indexAnswer = Integer.parseInt(myQuestion.getAnswer()) - 1;
for (int i = 0; i < possibleAnswers.length; i++) {
//if the button at position i was pressed
if (checkedId == possibleAnswers[i].getId()) {
//if its the correct answer
if (i == indexAnswer) {
Log.i("MyAcitvity", "thats correct");
possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.checkmark, 0);
}
else {
Log.i("MyActivity", "thats wrong");
possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.wrong, 0);
}
} else {
possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
}
}
});
谢谢你们,祝你们今天愉快。
【问题讨论】:
标签: java android radio-button drawable oncheckedchanged