zoukankan      html  css  js  c++  java
  • android中string.xml中%1$s、%1$d等的用法

    在TextView中想要动态的显示某些值,用到%1$s,%1$d,先介绍一下:

    %n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格 
    %n$md:代表输出的是整数,n代表是第几个参数,设置m的值可以在输出之前放置空格
    %n$mf:代表输出的是浮点数,n代表是第几个参数,设置m的值可以控制小数位数,如m=2.2时,输出格式为00.00 

    下面测试一下

    1、

    <string name="loading">离配置结束还剩%1$s秒</string>

    String temp = getResources().getString(R.string.loading);
    String timeTip = String.format(temp,60);

    结果:离配置结束还剩60秒

    2、

    <string name="loading">离配置结束还剩%1$3s秒</string>

    String temp = getResources().getString(R.string.loading);
    String timeTip = String.format(temp,60);

    结果:离配置结束还剩 60秒

    注:m设置为3只有1个空格

    3、

    <string name="loading">离配置结束还剩%1$3s秒</string>

    String temp = getResources().getString(R.string.loading);
    String timeTip = String.format(temp,60);

    结果:离配置结束还剩        60秒

    注:m设置为10,有8个空格

    4、

    <string name="loading">离配置结束还剩%1$#4s秒</string>

    String temp = getResources().getString(R.string.loading);
    String timeTip = String.format(temp,60);

    结果:app崩溃,抛出异常信息:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.stringtest/com.example.stringtest.MainActivity}: java.util.FormatFlagsConversionMismatchException: %s does not support '#'

    注:%s不支持设置#

    5、

    <string name="loading">离配置结束还剩%1$4d秒</string>

    String temp = getResources().getString(R.string.loading);
    String timeTip = String.format(temp,60);

    结果:离配置结束还剩  60秒

    注:m设置为4,有2个空格

    6、

    <string name="loading">离配置结束还剩%1$3.3f秒</string>

    String temp = getResources().getString(R.string.loading);
    String timeTip = String.format(temp,123456.123456);

    结果:离配置结束还剩123456.123秒

    注:m设置为3.3,小数位只取3位

  • 相关阅读:
    Linux网络编程--socket
    UDP学习总结
    TCP协议学习总结
    DNS协议总结
    DHCP协议总结
    ARP协议总结
    二层协议--MPLS协议总结
    二层协议--LLDP协议总结
    二层协议--LACP协议总结
    二层协议--STP协议总结
  • 原文地址:https://www.cnblogs.com/Eric-zhao/p/5230007.html
Copyright © 2011-2022 走看看