java - 在android中调用方法后如何恢复xml状态
问题描述
代码:
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文件中提到的颜色...这是默认按钮颜色
解决方案
在更新问题中,重置所有文本视图的颜色,因为您正在重用这些文本视图并且文本视图的状态将保持不变,因此请使用
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();
//-------------------
opt1.setBackgroundColor(Color.TRANSPARENT);
opt2.setBackgroundColor(Color.TRANSPARENT);
opt3.setBackgroundColor(Color.TRANSPARENT);
opt4.setBackgroundColor(Color.TRANSPARENT);
//-------------------
que.setText(question);
opt1.setText(option1);
opt2.setText(option2);
opt3.setText(option3);
opt4.setText(option4);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
推荐阅读
- javascript - 按下制表键时如何将光标定位在蒙版的开头?
- r - 如何在R(mlr)中反转学习者的factor.levels,因此不同问题之间的输出顺序相同
- javascript - 用jquery在一个句子中的两个单词下划线
- c# - 使用 ENTER(MVVM)将文本从 Textbox_Scan 添加到 TextBox_SerialBuffer 时出现问题
- reactjs - 如何在 ReactJs 中以编程方式设置 SCSS 变量值
- angular - TypeError:无法读取 null 的属性“getFullYear”(角度)
- http - 当我们有可用的 post 方法并且我们可以使用 post 方法执行相同的工作时,为什么我们使用 put patch delete?
- angular - 无法绑定到“chartType”,因为它不是 angular12 的“canvas”的已知属性
- android - 空指针异常尝试在空对象引用android studio上调用虚拟方法
- docker - Docker 撰写关键字