一个正常的记分牌的文字是这样的。
A normal scoreboard text is like this
所以我想使德文本格式,这样
so i want to make te text format like this
这是code
score = (TextView)findViewById(R.id.ct); score.setText(String.valueOf(gamescore)); if (gamescore > highscore){ high.setText(String.valueOf(gamescore)); }任何人都可以解释一下吗?感谢的
Anyone can explain? Thank's
推荐答案您应该使用的String.format()用的方法语法相对=nofollow>格式化语法。 的String.format()可用于左垫0的字符串,就像你想要的。
You should use the String.format() method along with the formatter syntax. String.format() can be used to left-pad a string with zeroes, just like you want.
这code会给你你想要的结果: high.setText(的String.format(%06D,gamescore));
This code will give you the results that you want: high.setText(String.format("%06d", gamescore));
让我们来看看具体是怎么回事。我们正在使用的语法如下:%[国旗] [宽度]转换
Let's look at what's going on in detail. We are using this syntax: %[flags][width]conversion
- 总是开始于一个%。 格式字符串语法
- 继%是 0 ,零填充标志。这告诉格式化,其结果将是零填充。
- 补零标志后是宽度,或者我们想要的最小位数;在这种情况下, 6 。
- 最后,指定转换类型。期望的结果被格式化为十进制整数,所以我们使用 D 。
- Always begin the format string syntax with a %.
- Following % is 0, the zero-padding flag. This tells the formatter that the result will be zero-padded.
- After the zero-padding flag is the width, or the minimum number of digits we want; in this case, 6.
- Lastly, specify the conversion type. The desired result is formatted as a decimal integer, so we use d.
下面是一个例子:
int gamescore = 100; String result = String.format("%06d", gamescore); System.out.println(result);输出: 000100
更多推荐
如何设置文本格式?
发布评论