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位

  • 相关阅读:
    win7每天出现taskeng.exe进程的解决方案
    hibernate插入中文字段时,无法插入数据库
    本页面用来演示如何通过JS SDK,创建完整的QQ登录流程,并调用openapi接口
    不同项目之间的通信
    404错误、405错误、500错误出错原因
    linux 下启动tomcat 时没有执行权限
    webservice文件上传下载(byte[] 实现方式)
    文件路径获取
    单个文件复制
    myeclipse svn重新定位 本地文件 svn 重新定位
  • 原文地址:https://www.cnblogs.com/Eric-zhao/p/5230007.html
Copyright © 2011-2022 走看看