如何在Android上设置计时器的时间格式?

时间:2014-12-23 20:54:52

标签: android countdowntimer

我正在研究Android应用程序,其中我想显示倒计时Android计时器的唯一秒数。例如仅持续10秒。我的代码如下:

long millis = millisUntilFinished;
String hms = String.format("%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)));

2 个答案:

答案 0 :(得分:1)

long millis = millisUntilFinished;
String secondsOnly = String.format("%02d", millis / 1000);

就是这样。如果您不希望它超过60,请使用:

long millis = millisUntilFinished;
String secondsOnly = String.format("%02d", (millis / 1000) % 60);

答案 1 :(得分:0)

只需检查以下代码,它就会100%帮助您。

new CountDownTimer(30000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
  //here you can have your logic to set text to edittext
 }

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