【问题标题】:Timer display to countdown定时器显示倒计时
【发布时间】:2014-01-30 03:50:50
【问题描述】:

现在我的计时器显示秒数,我可以添加什么来以 0:00 格式显示时间?

new CountDownTimer((300 * 1000), 1000) {

                         public void onTick(long millisUntilFinished) {
                             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                         }

                         public void onFinish() {
                             mTextField.setText("Session Completed!");
                         }
                      }.start();

【问题讨论】:

标签: android timer countdowntimer


【解决方案1】:

试试这个:

Date date = new Date((300 * 1000)* 1000);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String dateFormatted = formatter.format(date);

你也可以使用

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");

【讨论】:

  • 我有点困惑,你能把它应用到我一开始发布的倒计时吗?非常感谢!
  • 不,如果你只对使用 CountdownTimer 感兴趣,那么你必须只使用毫秒格式,但如果你想显示一个文本视图,那么在刻度内你有参数 millisUntilFinished,把它放在里面日期而不是 (300*1000)*1000 .....如果你得到你想要的,请投票并接受我的回答
【解决方案2】:
@Override
public void onTick(long millisUntilFinished) {
 long temp_long = millisUntilFinished / 1000;

 second = temp_long % 60;
 hour = temp_long / 3600;
 minute = (temp_long / 60) % 60;
 String tempTimer;

 tempTimer = ((hour < 10) ? "0" + hour : "" + hour)+ ((minute < 10) ? ":0" + minute : ":"+ minute)+ ((second < 10) ? ":0" + second : ":" + second);

 mTextField.setText(tempTimer);
}

【讨论】:

    【解决方案3】:

    您可以使用 android Chronometer 显示倒计时。

    http://developer.android.com/reference/android/widget/Chronometer.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      相关资源
      最近更新 更多