【问题标题】:How to set Text Format?如何设置文本格式?
【发布时间】:2016-03-31 09:35:10
【问题描述】:

一个普通的记分牌文本是这样的

所以我想制作这样的文本格式

这是代码

    score = (TextView)findViewById(R.id.ct);
    score.setText(String.valueOf(gamescore));

                    if (gamescore > highscore){
                        high.setText(String.valueOf(gamescore));
                     }

谁能解释一下?谢谢

【问题讨论】:

  • 使用String.format("%06d", gamescore)
  • @Mohit 这与那个问题一点也不相似。

标签: java android text textview


【解决方案1】:

您应该使用String.format() 方法和formatter syntaxString.format() 可用于左填充零的字符串,就像您想要的那样。

此代码将为您提供所需的结果:high.setText(String.format("%06d", gamescore));

让我们详细看看发生了什么。我们使用这种语法:%[flags][width]conversion

  • 格式字符串语法始终以 % 开头。
  • % 之后是0,零填充flag。这告诉格式化程序结果将被零填充。
  • 补零后flagwidth,或者我们想要的最小位数;在这种情况下,6
  • 最后,指定转换类型。所需结果被格式化为十进制整数,因此我们使用d

这是一个例子:

int gamescore = 100;
String result = String.format("%06d", gamescore);
System.out.println(result);

输出:000100

【讨论】:

  • 哦,谢谢米奇,它的工作!但是可以使用 Value Animator 对文本格式进行动画处理吗?
  • @Ricci 不幸的是,我从未使用过 ValueAnimator,所以我无法帮助您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多