每次重新运行程序时,如何将计时器重置为从00:00:00开始?

时间:2015-08-29 14:07:31

标签: java android

我添加到MainActvitiy.java

private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

然后在onCreate中我补充说: 并且当我运行程序时,计时器将自动启动,而无需单击开始按钮,我希望计时器在我运行程序时正确启动。

customHandler.postDelayed(updateTimerThread,0);

        timerValue = (TextView) findViewById(R.id.timerValue);

        startButton = (Button) findViewById(R.id.startButton);

        startButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                startTime = SystemClock.uptimeMillis();
                customHandler.postDelayed(updateTimerThread, 0);

            }
        });

        pauseButton = (Button) findViewById(R.id.pauseButton);

        pauseButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(updateTimerThread);

            }
        });

然后方法updateTimerThread:

private Runnable updateTimerThread = new Runnable() {

        public void run() {

            timeInMilliseconds = 0L;//SystemClock.uptimeMillis() - startTime;
            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            timerValue.setText("" + mins + ":"
                    + String.format("%02d", secs) + ":"
                    + String.format("%03d", milliseconds));
            customHandler.postDelayed(this, 0);
        }

    };

在这个方法中我做了:

timeInMilliseconds = 0L;

但它并没有太大改变。 我想要做的是每次我从头开始运行我的程序,计时器将从00:00:00开始

EDIT 在我现在做的活动中:

    startTime = SystemClock.uptimeMillis();
    customHandler.postDelayed(updateTimerThread,0);
    timerValue = (TextView) findViewById(R.id.timerValue);

    startButton = (Button) findViewById(R.id.startButton);

    startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);

        }
    });

    pauseButton = (Button) findViewById(R.id.pauseButton);

    pauseButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);

        }
    });

在upadteTimerThread中我没有改变:

private Runnable updateTimerThread = new Runnable() {

        public void run() {

            timeInMilliseconds = System.currentTimeMillis() - startTime;
            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            timerValue.setText("" + mins + ":"
                    + String.format("%02d", secs) + ":"
                    + String.format("%03d", milliseconds));
            customHandler.postDelayed(this, 0);
        }

    };

仍在运行程序时,计时器不会从00:00:00开始,但我会在会议记录中看到一个很长的数字也在第二个,就像它继续没有像重新开始那样重新开始。

2 个答案:

答案 0 :(得分:2)

startButton onClick方法中,您有:

startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);

但在顶部,你只有:

customHandler.postDelayed(updateTimerThread,0);

由于updateTimerThread使用startTime值,您很可能希望在顶部以相同的方式初始化它。

答案 1 :(得分:0)

将计时器重置为零的关键是updatedTime变量。这是确定按下“开始”按钮时计时器启动的位置。

由于startTime = SystemClock.uptimeMillis();已经正确地将startTime设置回0,因此无需重新初始化startTime变量。请记住startTime与当前在计时器上显示的内容相关。这就是为什么定时器从你暂停的地方开始,并且没有跳过定时器暂停的秒数。

timeSwapBuff事件中将0L设置回onClick作为开始按钮。这会将时间缓冲区重置为0.然后将其添加回startTime(也为0)并强制计时器完全重新开始。

尝试:

public void onClick(View view) {
    timeSwapBuff = 0L;
    startTime = SystemClock.uptimeMillis();
    customHandler.postDelayed(updateTimerThread, 0);
}