【发布时间】:2018-05-21 08:20:21
【问题描述】:
代码:
private void updateQuestion() {
mDatabaseReference.child("Users").child(RecieversId).child("Quiz").child("Question" + mQuestionNumber).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.child("Question").getValue().toString();
answer = dataSnapshot.child("Answer").getValue().toString();
option1 = dataSnapshot.child("Option1").getValue().toString();
option2 = dataSnapshot.child("Option2").getValue().toString();
option3 = dataSnapshot.child("Option3").getValue().toString();
option4 = dataSnapshot.child("Option4").getValue().toString();
que.setText(question);
opt1.setText(option1);
opt2.setText(option2);
opt3.setText(option3);
opt4.setText(option4);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
opt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mQuestionNumber++;
updateQuestion();
qn.setText("Quesion : " + mQuestionNumber);
if (option1.equals(answer)) {
opt1.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
updateQuestion();
} else
opt1.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
updateQuestion();
}
});
opt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mQuestionNumber++;
updateQuestion();
qn.setText("Quesion : " + mQuestionNumber);
if (option2.equals(answer)) {
opt2.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
updateQuestion();
} else
opt2.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
updateQuestion();
}
});
opt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mQuestionNumber++;
qn.setText("Quesion : " + mQuestionNumber);
if (option3.equals(answer)) {
opt3.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
updateQuestion();
} else
opt3.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
updateQuestion();
}
});
opt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mQuestionNumber++;
qn.setText("Quesion : " + mQuestionNumber);
if (option4.equals(answer)) {
opt4.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
updateQuestion();
} else
opt4.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
updateQuestion();
}
});
}
}
只是尝试制作一个测验应用程序... updatequestion 在每次按钮单击后调用(在用户选择答案后)...如果正确,则按钮变为绿色,如果错误则变为红色...但即使在离开后颜色仍然存在到下一个问题...我如何使它获得xml文件中提到的颜色...这是默认按钮颜色
【问题讨论】:
标签: java android database firebase