【问题标题】:CountDownTimer seconds in XX formatXX 格式的 CountDownTimer 秒数
【发布时间】:2015-04-15 13:36:25
【问题描述】:

我有一个这样的 CountDownTimer:

                     public void onTick(long millisUntilFinished) {                      
                             tv1.setText(""+String.format("%d:%d", 
                                        TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
                                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));  
                     }`

但在秒数小于 10 时,textview 应该显示第二个字母为零。所以它看起来像“1:02”而不是“1:2”。

【问题讨论】:

  • 那你在说什么...if it's less than 10, then add a '0'?
  • 是的,我试过了,但没用;也许你可以帮助我

标签: android time countdown seconds


【解决方案1】:

部分来自Android Formatter docs,“用前导零填充数字”,这需要您指定宽度:

在格式字符串中,您的目标宽度为 2。要使用 0 填充,您需要在十进制数字 (d) 前面加上 0,从而产生:

String.format("%02d:%02d", decimal1, decimal2) ...

【讨论】:

  • StackOverflow 等通过使 Android 文档易于访问/经常链接到它们确实帮助了我。这里有很多很好的答案。祝你的项目好运。
猜你喜欢
  • 2012-09-09
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 2022-08-04
  • 1970-01-01
  • 2011-04-07
  • 1970-01-01
  • 2019-08-25
相关资源
最近更新 更多