简体   繁体   English

在textView中格式化android文本格式的问题

[英]Issue with formatting android text in textView

I am having a lot of issues with formatting a string on a textView. 我在格式化textView上的字符串时遇到很多问题。 I am creating a stopwatch app and I need to format it as "00:00:00" but when I start the stopwatch, it comes up as "00:00:000" 我正在创建一个秒表应用,我需要将其格式化为“ 00:00:00”,但是当我启动秒表时,它会显示为“ 00:00:000”

Here is the code 这是代码

private Runnable updateTimerThread = new Runnable() {
public void run() {
  timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
  updatedTime = timeSwapBuff + timeInMilliseconds;
  int secs = (int) (updatedTime / 1000);
  int mins = secs / 60;
  secs = secs % 60;
  int milliseconds = (int) (updatedTime % 1000);
  timerValue.setText(String.format("%02d:%02d:%02d", mins, secs, milliseconds));
  customHandler.postDelayed(this, 0);
}

}; };

Let me know if you need more info. 让我知道您是否需要更多信息。

Thanks in advance 提前致谢

I believe the fix you want is to make milliseconds 3 digits as it can be that many digits: 我相信您要解决的问题是使毫秒数变为3位数字,因为它可以是很多数字:

 "%02d:%02d:%03d"

"%02d" prepends zeros if the number is too short, but does not truncate it if it is too long. 如果数字太短,“%02d”会以零开头,但是如果数字太长,则不会截断它。 Milliseconds can be a 3 digit value. 毫秒可以是3位数的值。

If you want the final value to be 2 digits you can do: 如果希望最终值为2位数字,则可以执行以下操作:

  milliseconds /= 10;  // now 1/100ths of a second

Isn't Milliseconds in 1/1000th of a second? 毫秒不是1/1000秒吗? I think you need to displayed up to hundreth.. I think 00:00:000 is accurate in this case. 我认为您需要显示的最深处。.我认为在这种情况下00:00:000是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM