-1

使用几个教程,我设法在下面创建了倒数计时器 - 这让我为自己感到自豪:) 现在我希望这个计时器也能够启动表格,如果它被停止而不是从头开始重新开始?欢迎所有想法//链接/教程。

先感谢您。

    setTime(0);

}

private void setTime(int seconds) {
    // TODO Auto-generated method stub

    Time = seconds;
    int minutes = seconds / 60;
    seconds = seconds % 60;

    tvCzas.setText(String.format("%02d:%02d", minutes, seconds));

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == bAddSec)
        setTime(Time + 1);
    else if (v == bTakeSec)
        setTime(Time - 1);
    else if (v == bAddMinute)
        setTime(Time + 60);
    else if (v == bTakeMinute)
        setTime(Time - 60);
    else if (v == bReset)
        setTime(0);
    else if (v == bStart)
        if (mierzy)
            zatrzym();
        else
            rozpocznij();

}

private void rozpocznij() {
    // TODO Auto-generated method stub

    Timer = new CountDownTimer(Time * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            int millis = (int) millisUntilFinished;
            int seconds = (int) millisUntilFinished / 1000;
            int minutes = seconds / 60;
            millis = millis % 100;
            seconds = seconds % 60;
            tvCzas.setText(String.format("%02d:%02d", minutes, seconds));
            bStart.setText("Stop");

        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            mierzy = false;
            setCount(Licznik + 1);

            tvCzas.setText("Koniec!");
            bStart.setText("Start");
        }
    };

    Timer.start();
    bStart.setText("Stop");
    mierzy = true;
}

private void zatrzym() {
    // TODO Auto-generated method stub
    if (Timer != null)
        Timer.cancel();

    mierzy = false;
    bStart.setText("Start");

}

}

4

1 回答 1

0

使用 onSaveInstanceState 和 onRestoreInstanceState 来存储您的计时器状态...有关用法,请参阅此答案

于 2013-02-23T13:02:11.930 回答