【问题标题】:Issue with formatting android text in textView在 textView 中格式化 android 文本的问题
【发布时间】:2014-09-04 22:56:13
【问题描述】:

我在格式化 textView 上的字符串时遇到了很多问题。我正在创建一个秒表应用程序,我需要将其格式化为“00:00:00”,但是当我启动秒表时,它显示为“00:00:000”

这里是代码

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);
}

};

如果您需要更多信息,请告诉我。

提前致谢

【问题讨论】:

    标签: android string formatting textview formatter


    【解决方案1】:

    我相信您想要的解决方法是将毫秒数设为 3 位数,因为它可以是很多位数:

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

    "%02d" 如果数字太短,则在前面添加零,但如果数字太长,则不会截断它。毫秒可以是 3 位数值。

    如果您希望最终值为 2 位数字,您可以这样做:

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

    【讨论】:

      【解决方案2】:

      毫秒不是 1/1000 秒吗? 我认为你需要展示到数百个.. 我认为 00:00:000 在这种情况下是准确的。

      【讨论】:

        猜你喜欢
        • 2016-12-15
        • 2020-05-23
        • 2020-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多