CountDownTimer以00:00:00格式显示

时间:2013-09-27 12:14:07

标签: android

我有一个倒计时器,用于读取时间并在特定时间停止。 显示使用时间和剩余时间。我在我的活动中编了一个样本。但是假设帮助我以00:00:00格式显示的功能效果不佳。当计时器停止时,它只以该格式显示它。

    public class PracticeQuestionActivity extends SherlockActivity implements OnClickListener {

private long timeElapsed;
    private boolean timerHasStarted = false;

    private final long startTime = 50000;
    private final long interval = 1000;

    MyCountDownTimer countdown = null;
    TextView timerText = null, ElaspedTime = null;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.practicequestion);

        context = PracticeQuestionActivity.this;

        initializeComponents(context);
        countdown = new MyCountDownTimer(startTime,interval);
        this.controlTimer(); //This is going to start the timer
    }

    private void controlTimer(){

        if (!timerHasStarted)
        {
            countdown.start();
            timerHasStarted = true;            
        }
    else
        {
            countdown.cancel();
            timerHasStarted = false;            
        }
    }

    //This is going to format the time value from duration in second
    private String setTimeFormatFromSeconds(long durationSeconds){

        return String.format("%02d:%02d:%02d", durationSeconds / 3600, (durationSeconds % 3600) / 60, (durationSeconds % 60));
    }


        //This method is going to be used to initialize the components of the view
    private void initializeComponents(Context context){

        timerText = (TextView)findViewById(R.id.timer);
        ElaspedTime = (TextView)findViewById(R.id.timeElapsed);

    }

     // CountDownTimer class
      public class MyCountDownTimer extends CountDownTimer
          {

              public MyCountDownTimer(long startTime, long interval)
                  {
                      super(startTime, interval);
                  }

              @Override
              public void onFinish()
                  {
                      timerText.setText("Time's up!");
                      ElaspedTime.setText("Time Elapsed: " + setTimeFormatFromSeconds(startTime));
                  }

              @Override
              public void onTick(long millisUntilFinished)
                  {
                      timerText.setText("Time remain:" + millisUntilFinished);
                      timeElapsed = startTime - millisUntilFinished;
                      ElaspedTime.setText("Time Elapsed: " + String.valueOf(timeElapsed));
                  }
          }

    }

2 个答案:

答案 0 :(得分:1)

看看下面的代码 -

timeInMillies = SystemClock.uptimeMillis() - startTime;
   finalTime = timeSwap + timeInMillies;

   int seconds = (int) (finalTime / 1000);
   int minutes = seconds / 60;
   seconds = seconds % 60;
   int milliseconds = (int) (finalTime % 1000);
   textTimer.setText("" + minutes + ":"
     + String.format("%02d", seconds) + ":"
     + String.format("%03d", milliseconds));
   myHandler.postDelayed(this, 0);

了解详情:http://www.androidhub4you.com/2013/04/countdown-demo-in-android-custom-timer.html#ixzz2g65Y77ff

答案 1 :(得分:0)

SimpleDateFormat是你的朋友:

private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

@Override 
public void onFinish() {
     timerText.setText("Time's up!");
     ElaspedTime.setText("Time Elapsed: " + timeFormat.format(startTime);
}

@Override
public void onTick(long millisUntilFinished) {
    timerText.setText("Time remain:" + timeFormat.format(millisUntilFinished));
    timeElapsed = startTime - millisUntilFinished;
    ElaspedTime.setText("Time Elapsed: " + timeFormat.format(timeElapsed));
}