2014-09-04 54 views
0

我在格式化textView上的字符串時遇到了很多問題。我創建一個秒錶應用程序,我需要把它格式化爲「00:00:00」,但是當我啓動秒錶,說到爲「00:00:000」在textView中格式化android文本問題

下面是代碼

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

};

讓我知道你是否需要更多信息。

在此先感謝

回答

3

我相信你想要的解決方法是讓毫秒3個數字,因爲它可以是很多的數字:

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

「%02D」預規劃的零,如果數字是太短,但如果它太長,則不會截斷它。毫秒可以是3位數值。

如果你想最終值是2個位數,你可以這樣做:

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

是不是毫秒秒的1/1/1000? 我認爲你需要展示給hundreth .. 我認爲00:00:000在這種情況下是準確的。