Java 方法跳过以执行某些代码
代码 它的行为很奇怪。当我得到第一个问题,我选择了一个错误的答案,它会毫无问题地进入下一个问题。但当我选择了正确的答案时,它会跳过下一个问题并转到下一个问题。例如,如果我在问题2中选择了正确答案,它将跳过问题3并显示问题4。这很奇怪,我不知道哪里出了错Java 方法跳过以执行某些代码,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,代码 它的行为很奇怪。当我得到第一个问题,我选择了一个错误的答案,它会毫无问题地进入下一个问题。但当我选择了正确的答案时,它会跳过下一个问题并转到下一个问题。例如,如果我在问题2中选择了正确答案,它将跳过问题3并显示问题4。这很奇怪,我不知道哪里出了错 我确信我的if语句中有一些错误,但我无法找出它是什么 在两个处理程序中,您都在调用 private void updateQuestion() { mDatabaseReference.child("Users").child(Reci
我确信我的if语句中有一些错误,但我无法找出它是什么 在两个处理程序中,您都在调用
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) {
if (option1.equals(answer)) {
opt1.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt1.setBackgroundColor(Color.CYAN);
}
}, 1500);
} else
opt1.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt1.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
opt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option2.equals(answer)) {
opt2.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt2.setBackgroundColor(Color.CYAN);
}
}, 1500);
} else
opt2.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt2.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
opt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option3.equals(answer)) {
opt3.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt3.setBackgroundColor(Color.CYAN);
}
}, 1500);
} else
opt3.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt3.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
opt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option4.equals(answer)) {
opt4.setBackgroundColor(Color.GREEN);
mScore++;
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt4.setBackgroundColor(Color.CYAN);
}
}, 1500);
} else
opt4.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt4.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});
}
}
如果答案正确,打两次电话
mQuestionNumber++;
updateQuestion();
解决方案是删除第一次//一次
处理程序
i、 e
如果条件为
更新opt1
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt1.setBackgroundColor(Color.CYAN);
}
}, 1500);
从每个if条件中删除“handler”if应该出现在哪里?我的意思是,如果没有如何确定答案是正确的,或者我们只是删除处理程序,就这样。如果只有这样的条件我能得到1个按钮的代码吗?因为你的答案和我的密码完全一样
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt1.setBackgroundColor(Color.CYAN);
}
}, 1500);
opt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (option1.equals(answer)) {
opt1.setBackgroundColor(Color.GREEN);
mScore++;
} else
opt1.setBackgroundColor(Color.RED);
sco.setText("Score : " + mScore);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mQuestionNumber++;
qn.setText("Question : " + mQuestionNumber);
updateQuestion();
opt1.setBackgroundColor(Color.CYAN);
}
}, 1500);
}
});