Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在活动中正确设置倒计时

请帮帮我,我有一个倒计时的方法,我需要暂停和恢复。 我设法用这个按钮暂停了计时器。onPause中的cancel()但是我无法让它正确重新启动。 我有两种情况: 场景1——它会暂停,但在重新启动时,它似乎会重新启动两个倒计时计时器,即使初始计时器被取消

    public void playAgain (View view) {

        score = 0;
        numberOfQuestions = 0;
        timerTextView.setText("60s");
        pointsTextView.setText("0/0");
        resultTextView.setText("");
        playAgainButton.setVisibility(View.INVISIBLE);
        returnToMenu.setVisibility(View.INVISIBLE);
        resultDisplayTextView.setVisibility(View.INVISIBLE);
        countDownText.setVisibility(View.VISIBLE);
        button0.setVisibility(View.INVISIBLE);
        button1.setVisibility(View.INVISIBLE);
        button2.setVisibility(View.INVISIBLE);
        button3.setVisibility(View.INVISIBLE);
        sumTextView.setVisibility(View.INVISIBLE);

            countDownTimer = new CountDownTimer(millisInFuture, countDownInterval) {
                @Override
                public void onTick(long timeRemain) {

                    millisInFuture = timeRemain;

                    countDownText.setText(String.valueOf(timeRemain / 1000 - 60));

                    if (timeRemain <= 61000) {

                        timerTextView.setText(String.valueOf(timeRemain / 1000) + "s");
                        button0.setEnabled(true);
                        button1.setEnabled(true);
                        button2.setEnabled(true);
                        button3.setEnabled(true);
                        button0.setVisibility(View.VISIBLE);
                        button1.setVisibility(View.VISIBLE);
                        button2.setVisibility(View.VISIBLE);
                        button3.setVisibility(View.VISIBLE);
                        sumTextView.setVisibility(View.VISIBLE);
                        countDownText.setVisibility(View.INVISIBLE);
                    }

                }


                @Override
                public void onFinish() {

                    timerTextView.setText("0s");



                    if (score > 0) {
                        percentage = score * 100 / numberOfQuestions;
                    } else {
                        percentage = 0;
                    }

                    button0.setEnabled(false);
                    button1.setEnabled(false);
                    button2.setEnabled(false);
                    button3.setEnabled(false);
                    playAgainButton.setVisibility(View.VISIBLE);
                    returnToMenu.setVisibility(View.VISIBLE);
                    resultDisplayTextView.setVisibility(View.VISIBLE);
                    resultTextView.setText(" ");
    }
            }.start();

        generateQuestion();

    }


 @Override
    public void onPause() {

        countDownTimer.cancel();

        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
CountDownTimer countDownTimer1 = new CountDownTimer(millisInFuture, countDownInterval) {
            @Override
            public void onTick(long timeRemain) {

                millisInFuture = timeRemain;

                countDownText.setText(String.valueOf(timeRemain / 1000 - 60));

                if (timeRemain <= 61000) {

                    timerTextView.setText(String.valueOf(timeRemain / 1000) + "s");
                    button0.setEnabled(true);
                    button1.setEnabled(true);
                    button2.setEnabled(true);
                    button3.setEnabled(true);
                    button0.setVisibility(View.VISIBLE);
                    button1.setVisibility(View.VISIBLE);
                    button2.setVisibility(View.VISIBLE);
                    button3.setVisibility(View.VISIBLE);
                    sumTextView.setVisibility(View.VISIBLE);
                    countDownText.setVisibility(View.INVISIBLE);
                }

            }


            @Override
            public void onFinish() {

                timerTextView.setText("0s");



                if (score > 0) {
                    percentage = score * 100 / numberOfQuestions;
                } else {
                    percentage = 0;
                }

                button0.setEnabled(false);
                button1.setEnabled(false);
                button2.setEnabled(false);
                button3.setEnabled(false);
                playAgainButton.setVisibility(View.VISIBLE);
                returnToMenu.setVisibility(View.VISIBLE);
                resultDisplayTextView.setVisibility(View.VISIBLE);
                resultTextView.setText(" ");
}
        }.start();
}

第二种情况是系统崩溃:

    public void playAgain (View view) {

        score = 0;
        numberOfQuestions = 0;
        timerTextView.setText("60s");
        pointsTextView.setText("0/0");
        resultTextView.setText("");
        playAgainButton.setVisibility(View.INVISIBLE);
        returnToMenu.setVisibility(View.INVISIBLE);
        resultDisplayTextView.setVisibility(View.INVISIBLE);
        countDownText.setVisibility(View.VISIBLE);
        button0.setVisibility(View.INVISIBLE);
        button1.setVisibility(View.INVISIBLE);
        button2.setVisibility(View.INVISIBLE);
        button3.setVisibility(View.INVISIBLE);
        sumTextView.setVisibility(View.INVISIBLE);

            countDownTimer = new CountDownTimer(millisInFuture, countDownInterval) {
                @Override
                public void onTick(long timeRemain) {

                    millisInFuture = timeRemain;

                    countDownText.setText(String.valueOf(timeRemain / 1000 - 60));

                    if (timeRemain <= 61000) {

                        timerTextView.setText(String.valueOf(timeRemain / 1000) + "s");
                        button0.setEnabled(true);
                        button1.setEnabled(true);
                        button2.setEnabled(true);
                        button3.setEnabled(true);
                        button0.setVisibility(View.VISIBLE);
                        button1.setVisibility(View.VISIBLE);
                        button2.setVisibility(View.VISIBLE);
                        button3.setVisibility(View.VISIBLE);
                        sumTextView.setVisibility(View.VISIBLE);
                        countDownText.setVisibility(View.INVISIBLE);
                    }

                }


                @Override
                public void onFinish() {

                    timerTextView.setText("0s");



                    if (score > 0) {
                        percentage = score * 100 / numberOfQuestions;
                    } else {
                        percentage = 0;
                    }

                    button0.setEnabled(false);
                    button1.setEnabled(false);
                    button2.setEnabled(false);
                    button3.setEnabled(false);
                    playAgainButton.setVisibility(View.VISIBLE);
                    returnToMenu.setVisibility(View.VISIBLE);
                    resultDisplayTextView.setVisibility(View.VISIBLE);
                    resultTextView.setText(" ");
    }
            }.start();

        generateQuestion();

    }


 @Override
    public void onPause() {

        countDownTimer.cancel();

        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
countDownTimer.start();
}

我在谷歌上搜索过,似乎找不到有效的解决方案。 请帮助,Galaxy store拒绝上传我的apk,除非此功能有效


共 (0) 个答案