【问题标题】:Android, Is it possible to format textview like System.out.format("%02d", n);?Android,是否可以像 System.out.format("%02d", n); 这样格式化 textview?
【发布时间】:2012-06-04 07:27:30
【问题描述】:

我有一个包含不同列的表。而不是为每个项目创建文本视图,我希望每一行都有一个文本视图。我想将每个项目一一添加到行字符串中。因为有些项目可能有 1 位数字,而另一些项目可能有两位数字,我想格式化它。

在 Java 中,我们会这样做:

intn = 7;
System.out.format("%0d", n);      //  -->  "07"

是否可以用 textview 做同样的事情?如果是的话怎么做。谢谢

【问题讨论】:

    标签: android textview format


    【解决方案1】:

    http://developer.android.com/guide/topics/resources/string-resource.html

    格式化字符串

    <string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
    
    Resources res = getResources();
    String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
    

    【讨论】:

    【解决方案2】:

    有可能,参考this

    String text = String.format("%0d", n);
    textView.setText(text);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多