倒数计时器 - 切换到横向模式时重置时间

时间:2017-11-20 12:28:35

标签: java android countdowntimer

我是Android开发的新手。我在下面尝试做的是创建savedInstanceState到我的倒计时变量,但失败了。 除了第13 - 14 - 15 - 50 - 51 - 52 - 70 - 71 - 72之外,所有代码都正常工作。这些代码什么都不做。如何让这个应用程序在切换到横向模式时不重置计时器?

package com.example.android.scorekeeperapp;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    static final String stateIsPaused = "emergencyIsPaused";
    static final String stateIsCanceled = "emergencyIsCanceled";
    static final String stateRemainingTime = "emergencyRemainingTime";

    static final String stateScoreTeamA = "emergencyScoreA";
    static final String stateScoreTeamB = "emergencyScoreB";
    static final String stateFoulCounterA = "emergencyFoulA";
    static final String stateFoulCounterB = "emergencyFoulB";
    static final String stateScoreThreePointTeamA = "emergencyThreeA";
    static final String stateScoreThreePointTeamB = "emergencyThreeB";
    static final String stateScoreTwoPointTeamA = "emergencyTwoA";
    static final String stateScoreTwoPointTeamB = "emergencyTwoA";
    static final String stateScoreOnePointTeamA = "emergencyOneA";
    static final String stateScoreOnePointTeamB = "emergencyOneA";

    private TextView tv1;
    private Button startbtn, cancelbtn;
    private ToggleButton togbtn;

    private boolean isPaused = false;
    private boolean isCanceled = false;
    private long remainingTime = 0;

    int scoreTeamA = 0;
    int scoreTeamB = 0;
    int foulCounterA = 0;
    int foulCounterB = 0;
    int scoreThreePointTeamA = 0;
    int scoreThreePointTeamB = 0;
    int scoreTwoPointTeamA = 0;
    int scoreTwoPointTeamB = 0;
    int scoreOnePointTeamA = 0;
    int scoreOnePointTeamB = 0;

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {

        savedInstanceState.putBoolean(stateIsPaused, isPaused);
        savedInstanceState.putBoolean(stateIsCanceled, isCanceled);
        savedInstanceState.putLong(stateRemainingTime, remainingTime);

        savedInstanceState.putInt(stateScoreTeamA, scoreTeamA);
        savedInstanceState.putInt(stateScoreTeamB, scoreTeamB);
        savedInstanceState.putInt(stateFoulCounterA, foulCounterA);
        savedInstanceState.putInt(stateFoulCounterB, foulCounterB);
        savedInstanceState.putInt(stateScoreThreePointTeamA, scoreThreePointTeamA);
        savedInstanceState.putInt(stateScoreThreePointTeamB, scoreThreePointTeamB);
        savedInstanceState.putInt(stateScoreTwoPointTeamA, scoreTwoPointTeamA);
        savedInstanceState.putInt(stateScoreTwoPointTeamB, scoreTwoPointTeamB);
        savedInstanceState.putInt(stateScoreOnePointTeamA, scoreOnePointTeamA);
        savedInstanceState.putInt(stateScoreOnePointTeamB, scoreOnePointTeamB);
        super.onSaveInstanceState(savedInstanceState);
    }

    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        isPaused = savedInstanceState.getBoolean(stateIsPaused);
        isCanceled = savedInstanceState.getBoolean(stateIsCanceled);
        remainingTime = savedInstanceState.getLong(stateRemainingTime);

        scoreTeamA = savedInstanceState.getInt(stateScoreTeamA);
        scoreTeamB = savedInstanceState.getInt(stateScoreTeamB);
        foulCounterA = savedInstanceState.getInt(stateFoulCounterA);
        foulCounterB = savedInstanceState.getInt(stateFoulCounterB);
        scoreThreePointTeamA = savedInstanceState.getInt(stateScoreThreePointTeamA);
        scoreThreePointTeamB = savedInstanceState.getInt(stateScoreThreePointTeamB);
        scoreTwoPointTeamA = savedInstanceState.getInt (stateScoreTwoPointTeamA);
        scoreTwoPointTeamB = savedInstanceState.getInt (stateScoreTwoPointTeamB);
        scoreOnePointTeamA = savedInstanceState.getInt (stateScoreOnePointTeamA);
        scoreOnePointTeamB = savedInstanceState.getInt (stateScoreOnePointTeamB);
        displayForTeamA(scoreTeamA);
        displayForTeamAThreePoint (scoreThreePointTeamA);
        displayForTeamATwoPoint(scoreTwoPointTeamA);
        displayForTeamAOnePoint(scoreOnePointTeamA);
        displayForTeamAFoul(foulCounterA);
        displayForTeamB(scoreTeamB);
        displayForTeamBThreePoint(scoreThreePointTeamB);
        displayForTeamBTwoPoint(scoreTwoPointTeamB);
        displayForTeamBOnePoint(scoreOnePointTeamB);
        displayForTeamBFoul(foulCounterB);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv1 = (TextView) findViewById(R.id.tv1);
        startbtn = (Button) findViewById(R.id.startbtn);
        cancelbtn = (Button) findViewById(R.id.cancelbtn);
        togbtn = (ToggleButton) findViewById(R.id.togbtn);

        cancelbtn.setEnabled(false);
        togbtn.setEnabled(false);

        startbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startbtn.setEnabled(false);
                cancelbtn.setEnabled(true);
                togbtn.setEnabled(true);

                isPaused = false;
                isCanceled = false;

                long millisInfuture = 20000; /////20sec
                long countDownInterval = 1000; /////1sec
                new CountDownTimer(millisInfuture, countDownInterval) {
                    @Override
                    public void onTick(long millisUntilFinished){
                        if(isPaused || isCanceled) {
                            cancel();
                        }else {
                            tv1.setText(""+millisUntilFinished/1000);
                            remainingTime = millisUntilFinished;
                        }

                    }

                    @Override
                    public void onFinish() {

                        tv1.setText("Times up!");

                    }
                }.start();
            }
        });

        togbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (togbtn.isChecked()) {
                    isPaused = true;
                }else {
                    isPaused = false;
                    long millisInfuture = remainingTime;
                    long countDownInterval = 1000; /////1sec
                    new CountDownTimer(millisInfuture, countDownInterval) {
                        @Override
                        public void onTick(long millisUntilFinished){
                            if(isPaused || isCanceled) {
                                cancel();
                            }else {
                                tv1.setText(""+millisUntilFinished/1000);
                                remainingTime = millisUntilFinished;
                            }

                        }

                        @Override
                        public void onFinish() {

                            tv1.setText("Times up!");

                        }
                    }.start();
                }
            }
        });

        cancelbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isCanceled = true;
                tv1.setText("Timer");
                startbtn.setEnabled(true);
                togbtn.setEnabled(false);
                cancelbtn.setEnabled(false);
            }
        });
    }

    public void threePointForTeamA(View v) {
        scoreTeamA = scoreTeamA + 3;
        scoreThreePointTeamA = scoreThreePointTeamA + 3;
        displayForTeamA(scoreTeamA);
        displayForTeamAThreePoint (scoreThreePointTeamA);
    }

    public void twoPointForTeamA(View v) {
        scoreTeamA = scoreTeamA + 2;
        scoreTwoPointTeamA = scoreTwoPointTeamA + 2;
        displayForTeamA(scoreTeamA);
        displayForTeamATwoPoint(scoreTwoPointTeamA);
    }

    public void onePointForTeamA(View v) {
        scoreTeamA = scoreTeamA + 1;
        scoreOnePointTeamA = scoreOnePointTeamA + 1;
        displayForTeamA(scoreTeamA);
        displayForTeamAOnePoint(scoreOnePointTeamA);
    }

    public void foulCountForTeamA(View v) {
        foulCounterA = foulCounterA + 1;
        displayForTeamAFoul(foulCounterA);
    }

    public void threePointForTeamB(View v) {
        scoreTeamB = scoreTeamB + 3;
        scoreThreePointTeamB = scoreThreePointTeamB + 3;
        displayForTeamB(scoreTeamB);
        displayForTeamBThreePoint(scoreThreePointTeamB);
    }

    public void twoPointForTeamB(View v) {
        scoreTeamB = scoreTeamB + 2;
        scoreTwoPointTeamB = scoreTwoPointTeamB + 2;
        displayForTeamB(scoreTeamB);
        displayForTeamBTwoPoint(scoreTwoPointTeamB);
    }

    public void onePointForTeamB(View v) {
        scoreTeamB = scoreTeamB + 1;
        scoreOnePointTeamB = scoreOnePointTeamB + 1;
        displayForTeamB(scoreTeamB);
        displayForTeamBOnePoint(scoreOnePointTeamB);
    }

    public void foulCountForTeamB(View v) {
        foulCounterB = foulCounterB + 1;
        displayForTeamBFoul(foulCounterB);
    }

    public void resetGame(View v) {
        scoreTeamA = 0;
        scoreThreePointTeamA = 0;
        scoreTwoPointTeamA = 0;
        scoreOnePointTeamA = 0;
        foulCounterA = 0;
        scoreTeamB = 0;
        scoreThreePointTeamB = 0;
        scoreTwoPointTeamB = 0;
        scoreOnePointTeamB = 0;
        foulCounterB = 0;
        displayForTeamA(scoreTeamA);
        displayForTeamAThreePoint(scoreThreePointTeamA);
        displayForTeamATwoPoint(scoreTwoPointTeamA);
        displayForTeamAOnePoint(scoreOnePointTeamA);
        displayForTeamAFoul(foulCounterA);
        displayForTeamB(scoreTeamB);
        displayForTeamBThreePoint(scoreThreePointTeamB);
        displayForTeamBTwoPoint(scoreTwoPointTeamB);
        displayForTeamBOnePoint(scoreOnePointTeamB);
        displayForTeamBFoul(foulCounterB);
    }

    public void displayForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_a_score);
        scoreView.setText(String.valueOf(score));
    }

    public void displayForTeamAThreePoint(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_a_score_3_point);
        scoreView.setText(String.valueOf(score));
    }

    public void displayForTeamATwoPoint(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_a_score_2_point);
        scoreView.setText(String.valueOf(score));
    }

    public void displayForTeamAOnePoint(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_a_score_1_point);
        scoreView.setText(String.valueOf(score));
    }

    public void displayForTeamAFoul(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_a_foul);
        scoreView.setText(String.valueOf(score));
    }

    public void displayForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_b_score);
        scoreView.setText(String.valueOf(score));
    }

    public void displayForTeamBThreePoint(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_b_score_3_point);
        scoreView.setText(String.valueOf(score));
    }

    public void displayForTeamBTwoPoint(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_b_score_2_point);
        scoreView.setText(String.valueOf(score));
    }

    public void displayForTeamBOnePoint(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_b_score_1_point);
        scoreView.setText(String.valueOf(score));
    }

    public void displayForTeamBFoul(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_b_foul);
        scoreView.setText(String.valueOf(score));
    }

}

1 个答案:

答案 0 :(得分:0)

您在remainingTime = millisUntilFinished;中设置CountDownTimer。您必须在remainingTime上获取onCreate并使用CountDownTimer创建millisInfuture - remainingTime 例如:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1 = (TextView) findViewById(R.id.tv1);
    startbtn = (Button) findViewById(R.id.startbtn);
    cancelbtn = (Button) findViewById(R.id.cancelbtn);
    togbtn = (ToggleButton) findViewById(R.id.togbtn);

    remainingTime = savedInstanceState.getLong(stateRemainingTime);

    cancelbtn.setEnabled(false);
    togbtn.setEnabled(false);

    startbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startbtn.setEnabled(false);
            cancelbtn.setEnabled(true);
            togbtn.setEnabled(true);

            isPaused = false;
            isCanceled = false;

            long millisInfuture = 20000 - remainingTime; /////20sec
            long countDownInterval = 1000; /////1sec
            new CountDownTimer(millisInfuture, countDownInterval) {
                 @Override
                 public void onTick(long millisUntilFinished){
                       if(isPaused || isCanceled) {
                           cancel();
                       }else {
                           tv1.setText(""+millisUntilFinished/1000);
                           remainingTime = millisUntilFinished;
                       }
                   }

                   @Override
                   public void onFinish() {
                       tv1.setText("Times up!");
                   }
               }.start();
           }
        });