Android的倒数计时器

时间:2016-08-23 15:30:18

标签: java android

这里有人可以帮我这个代码吗?它正在工作,但倒数计时器完成后,它会关闭我的应用程序。我希望它在文本上显示“完成”。但我使用的onFinish编码似乎不可行。

public class SimpleCountdowTimer extends CountDownTimer {
public static int oneSecond = 1000;
TextView statusTextView;
ProgressBar mprogressBar, mprogressBar1;
Button startTimerButton;
Button stopTimerButton, pauseTimerButton, resumeTimerButton;
private long timeRemaining = 0;

public SimpleCountdowTimer(long millisInFuture, long countDownInterval,
                           TextView statusTextView, ProgressBar mprogressBar, ProgressBar mprogressBar1) {

    super(millisInFuture, countDownInterval);

    this.statusTextView = statusTextView;
    this.mprogressBar1 = mprogressBar1;
    this.mprogressBar = mprogressBar;
}

@Override
public void onTick(long millisInFuture) {  //do something in every tick

        //Put count down timer remaining time in a variable
        statusTextView.setText(String.valueOf(millisInFuture/ oneSecond));
        long millis = millisInFuture;
        timeRemaining = millisInFuture;
        long countDownInterval = 1000;
        mprogressBar1.setProgress((int) (millisInFuture));
        //Convert milliseconds into hour,minute and seconds
        String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
        statusTextView.setText(hms);//set text
    }

@Override
public void onFinish() {
        statusTextView.setText("00:00:00");
        mprogressBar.setVisibility(View.VISIBLE);
        mprogressBar1.setVisibility(View.VISIBLE);
        //Disable the pause, resume and cancel button
        pauseTimerButton.setEnabled(false);
        resumeTimerButton.setEnabled(false);
        stopTimerButton.setEnabled(false);
        //Enable the start button
        startTimerButton.setEnabled(true);
}
}

1 个答案:

答案 0 :(得分:0)

Why don't you use this way : 

    CountDownTimer cdt = new CountDownTimer(10000, 1000) {

            @Override 
            public void onTick(long millisUntilFinished) {
                statusTextView.setText(String.valueOf(millisInFuture/ oneSecond));
        long millis = millisInFuture;
        timeRemaining = millisInFuture;
        long countDownInterval = 1000;
        mprogressBar1.setProgress((int) (millisInFuture));
        //Convert milliseconds into hour,minute and seconds 
        String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
        statusTextView.setText(hms);//set text
            } 

            @Override 
            public void onFinish() { 
                 statusTextView.setText("00:00:00");
        mprogressBar.setVisibility(View.VISIBLE);
        mprogressBar1.setVisibility(View.VISIBLE);
        //Disable the pause, resume and cancel button 
        pauseTimerButton.setEnabled(false);
        resumeTimerButton.setEnabled(false);
        stopTimerButton.setEnabled(false);
        //Enable the start button 
        startTimerButton.setEnabled(true);
            } 
        }; 

        cdt.start();