【问题标题】:Best way to run a function 10 times, at 10 second intervals以 10 秒为间隔运行 10 次函数的最佳方法
【发布时间】: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


【解决方案1】:

好的,经过一番挖掘并被 cmets 指出大致方向后,我发现正确的答案是处理程序。

我的 for 循环现在看起来像这样

for (int i = 0; i < 10; i++) {

        handler.postDelayed(() -> playGame(), 10000 * i);
        playerAnswered = false;
        playerScore += currentGame.checkPlayerAnswer(currentGame.playerOneSelection);

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2016-04-16
    • 2021-05-08
    • 2016-03-28
    • 2022-01-11
    相关资源
    最近更新 更多