我已经通过数组添加了5个问题,但每次我的问题重复。 我想我必须使用循环,但我不知道如何使用循环。 任何人都可以建议我如何使用循环并停止重复我的问题。
private Question[] mQuestionBank = new Question[]{
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_africa, false),
new Question(R.string.question_americas, true),
new Question(R.string.question_asia, true),
};
private int mCurrentIndex = 0;
private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
if (userPressedTrue == answerIsTrue) {
righttoast();
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
} else {
wrongtoast();
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
{
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkAnswer(true);
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkAnswer(false);
}
});
mNextButton = (Button) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
mpreviousButton = (Button) findViewById(R.id.previous_button);
mpreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mCurrentIndex == 0) {
return;
}
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
updateQuestion();
}
});
}
public void righttoast() {
LayoutInflater li = getLayoutInflater();
View layout = li.inflate(R.layout.righttoast, (ViewGroup) findViewById(R.id.right_toast_layout));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setView(layout);
toast.show();
}
public void wrongtoast() {
LayoutInflater li = getLayoutInflater();
View layout = li.inflate(R.layout.wrongtoast, (ViewGroup) findViewById(R.id.wrong_toast_layout));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setView(layout);
toast.show();
}
}
答案 0 :(得分:0)
将updateQuestion
方法更改为:
private void updateQuestion() {
if(mCurrentIndex < mQuestionBank.length()){
int question = mQuestionBank[mCurrentIndex].getTextResId();
mCurrentIndex++;
mQuestionTextView.setText(question);
}
}
然后将checkAnswer
方法更改为:
private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
if (userPressedTrue == answerIsTrue) {
righttoast();
updateQuestion();
} else {
wrongtoast();
updateQuestion();
}
}