getString(int resId, Object... formatArgs)的用法

来源:互联网 发布:我的淘宝店铺链接地址 编辑:程序博客网 时间:2024/05/22 12:54

public final String getString (int resId,Object... formatArgs)
Return a localized formatted string from the application's package's default string table, substituting the format arguments as defined inFormatter andformat(String, Object...).
官方文档地址:http://developer.android.com/reference/android/content/Context.html#getString%28int,%20java.lang.Object...%29

使用示例:


1. %s对应字符串
String tiptext =getString(R.string.format_error,"用户名","昵称","密码");

strings.xml 中 format_error 为:

<string name="format_error">请使用%1$s:%2$s:%3$s的格式</string>

返回结果: tiptext ="请使用用户名:昵称:密码的格式";


2.%d 对应整数
String tiptext =getString(R.string.photo_select_complete, 3,10));

strings.xml 中 photo_select_complete 为:

<string name="photo_select_complete">完成(%1$d/%2$d)</string>

返回结果: tiptext ="完成(3/10)";

注意:%d 对应整数 %s对应字符串

3 0
原创粉丝点击