我有一个倒数计时器,它显示从 60 到 0 的秒数(1 分钟倒数计时器)。当它达到 1 位数字时,例如 9,8,7.. 它显示 9 而不是 09。我尝试使用 String.format("%[B]02d[/B]", x);
我将 x 从 long 转换为 string。没用。
我想要 String.format("%2d", 1)
最佳答案
您可以使用 DecimalFormat 来完成它:
NumberFormat f = new DecimalFormat("00");
long time = 9;
textView.setText(f.format(time));
输出:
09
或者您可以使用 String.format()还有:
String format = "%1$02d"; // two digits
textView.setText(String.format(format, time));
https://stackoverflow.com/questions/20293887/