【发布时间】:2021-07-05 19:41:48
【问题描述】:
基本上,我有一个为琐事游戏加载问题的功能。每场比赛有10轮。目前我有一个四循环并尝试使用 CountDownTimer 但它仍然会迭代并立即调用该函数 10 次。只是在寻找一些关于如何调用持续 10 秒的回合然后进入下一轮的建议。
public class GameActivity extends AppCompatActivity {
int questionCount = 0;
int textUpdate = 0;
Boolean readFlag = true;
Button answerOneBtn, answerTwoBtn, answerThreeBtn, answerFourBtn;
TextView questionTextView;
int playerScore = 0;
Game currentGame = new Game(questionCount, readFlag, playerScore);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
currentGame.questionList.AnswersJumbled();
for(int i = 0; i < 10; i++) {
Log.e("Error", "For Loop is Running");
playGame();
}
answerOneBtn = findViewById(R.id.AnswerOneButton);
answerTwoBtn = findViewById(R.id.AnswerTwoButton);
answerThreeBtn = findViewById(R.id.AnswerThreeButton);
answerFourBtn = findViewById(R.id.AnswerFourButton);
questionTextView = findViewById(R.id.questionText);
answerOneBtn.setOnClickListener(v -> {
currentGame.playerOneSelection = answerOneBtn.getText().toString();
Log.e("error", currentGame.playerOneSelection);
});
answerTwoBtn.setOnClickListener(v -> {
currentGame.playerOneSelection = answerTwoBtn.getText().toString();
Log.e("error", currentGame.playerOneSelection);
});
answerThreeBtn.setOnClickListener(v -> {
currentGame.playerOneSelection = answerThreeBtn.getText().toString();
Log.e("error", currentGame.playerOneSelection);
});
answerFourBtn.setOnClickListener(v -> {
currentGame.playerOneSelection = answerFourBtn.getText().toString();
Log.e("error", currentGame.playerOneSelection);
});
}
public void playGame() {
answerOneBtn = findViewById(R.id.AnswerOneButton);
answerTwoBtn = findViewById(R.id.AnswerTwoButton);
answerThreeBtn = findViewById(R.id.AnswerThreeButton);
answerFourBtn = findViewById(R.id.AnswerFourButton);
questionTextView = findViewById(R.id.questionText);
currentGame.loadQuestion(questionCount);
questionTextView.setText(currentGame.currentQuestion);
answerOneBtn.setText(currentGame.firstAnswer);
answerTwoBtn.setText(currentGame.secondAnswer);
answerThreeBtn.setText(currentGame.thirdAnswer);
answerFourBtn.setText(currentGame.fourthAnswer);
questionCount++;
}
}
【问题讨论】:
-
@user15358848 我尝试了Timer,但无法让它工作,它仍然经历了几次。不知道我的实现出了什么问题
-
你应该使用
Handler。此外,查看您尝试过的代码对我们很有用,即使您认为它没有让您获得任何帮助,它显示了研究工作并帮助我们了解您想要实现的目标。
标签: java android loops class iteration