我正在创建测验应用,当用户选择答案时,我会根据他/她是否正确/不正确来更改单选按钮的背景颜色。我想这样做,暂停在这个部分停留一两秒,然后改变我片段中的问题/答案。问题是,我对我应该暂停的地方感到困惑。通常,我应该在onAnswerSelected()
函数(更改我的RadioButtons上的文本)执行之前将暂停放在正确的位置。但是,问题是屏幕上的任何内容都不会更新,直到onCheckedChanged()
完成执行。
Fragment.java
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
List<String> answers = new ArrayList<String>();
answers.add(answer1);
answers.add(answer2);
answers.add(answer3);
answers.add(answer4);
Collections.shuffle(answers);
TextView questionTV = (TextView) getView().findViewById(R.id.question);
questionTV.setText(question);
final RadioButton answer1RB = (RadioButton) getView().findViewById(R.id.answer1);
final RadioButton answer2RB = (RadioButton) getView().findViewById(R.id.answer2);
final RadioButton answer3RB = (RadioButton) getView().findViewById(R.id.answer3);
final RadioButton answer4RB = (RadioButton) getView().findViewById(R.id.answer4);
answer1RB.setText(answers.get(0));
answer2RB.setText(answers.get(1));
answer3RB.setText(answers.get(2));
answer4RB.setText(answers.get(3));
answer1RB.setChecked(false);
RadioGroup rg = (RadioGroup) getView().findViewById(R.id.answers);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
boolean correctAnswerSelected = false;
switch(checkedId)
{
case R.id.answer1:
if (answer1RB.getText().equals(answer1)) {
answer1RB.setBackgroundColor(Color.GREEN);
correctAnswerSelected = true;
} else {
answer1RB.setBackgroundColor(Color.RED);
findCorrectAnswer(answer1RB, answer2RB, answer3RB, answer4RB);
}
break;
case R.id.answer2:
if (answer2RB.getText().equals(answer1)) {
answer2RB.setBackgroundColor(Color.GREEN);
correctAnswerSelected = true;
} else {
answer2RB.setBackgroundColor(Color.RED);
findCorrectAnswer(answer1RB, answer2RB, answer3RB, answer4RB);
}
break;
case R.id.answer3:
if (answer3RB.getText().equals(answer1)) {
answer3RB.setBackgroundColor(Color.GREEN);
correctAnswerSelected = true;
} else {
answer3RB.setBackgroundColor(Color.RED);
findCorrectAnswer(answer1RB, answer2RB, answer3RB, answer4RB);
}
break;
case R.id.answer4:
if (answer4RB.getText().equals(answer1)) {
answer4RB.setBackgroundColor(Color.GREEN);
correctAnswerSelected = true;
} else {
answer4RB.setBackgroundColor(Color.RED);
findCorrectAnswer(answer1RB, answer2RB, answer3RB, answer4RB);
}
break;
}
// Need to execute some sort of pause here, and finish before onAnswerSelected is called
mCallback.onAnswerSelected(correctAnswerSelected);
}
});
}
QuizTemplate.java
@Override
public void onAnswerSelected(boolean answeredQuestionCorrectly) {
PlaceholderFragment fragment = (PlaceholderFragment)getSupportFragmentManager().
findFragmentById(R.id.container2);
if (fragment != null) {
RadioButton answer1 = (RadioButton) fragment.getView().findViewById(R.id.answer1);
RadioButton answer2 = (RadioButton) fragment.getView().findViewById(R.id.answer2);
RadioButton answer3 = (RadioButton) fragment.getView().findViewById(R.id.answer3);
RadioButton answer4 = (RadioButton) fragment.getView().findViewById(R.id.answer4);
answer1.setText("test");
answer2.setText("test");
answer3.setText("test");
answer4.setText("test");
answer1.setBackgroundColor(Color.WHITE);
answer2.setBackgroundColor(Color.WHITE);
answer3.setBackgroundColor(Color.WHITE);
answer4.setBackgroundColor(Color.WHITE);
answer1.setChecked(false);
answer2.setChecked(false);
answer3.setChecked(false);
answer4.setChecked(false);
}
答案 0 :(得分:0)
使用线程和runnable。睡眠线程暂停,然后更新UI。
示例代码:
new Thread(new Runnable() {
@Override
public void run() {
try {
//pause for 5 seconds
Thread.sleep(1000*5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// update your UI here
mCallback.onAnswerSelected(correctAnswerSelected);
}
});
}
}).start();