android - 格式化倒数计时器以在少于十秒时使用前导零数字

标签 android

我的倒数计时器需要以正确的格式显示秒数。目前一切正确,直到计数不到 10 秒。

此时它显示如下:9 8 7 6 5 4 3 2 1。我需要它的格式为 09 08 06 05 04 03 02 01

这是定时器代码:

private CountDownTimer timer(int time)
{
    CountDownTimer a = new CountDownTimer(time, 1000) 
    {
        public void onTick(long millisUntilFinished) {
            String clock;
            clock = "" + ((millisUntilFinished / 1000)/60)%60 + ":" + ((millisUntilFinished / 1000)%60);
            Clk.setText(clock);
            // mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            alarm.start();
            Clk.setText("00:00");
        }
    }.start();

    return a;
}

最佳答案

尝试使用 String.format

int remaining = ((millisUntilFinished / 1000)%60);
clock = String.format("seconds remaining: %02d .", remaining);

关于android - 格式化倒数计时器以在少于十秒时使用前导零数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19996459/

相关文章:

android - 将 json 数据的值存储到 arraylist 并填充 View 持有者

android - 如何在 Edittext 中获取光标位置

Android 支持带图像的多屏幕

Android 翻页标签故事

android - 找不到 com.android.support :appcompat-v7:24. 2.1

android - AutoCompleteTextView OnItemClick 位置始终为 "0"

Android 定义可在自定义启动器中使用的快捷方式

java - MPAndroidChart 饼图

Android - 将图像文件发送到服务器数据库

android - 在推送通知 Firebase 中发送 URL