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

Android Context.getString 方法详解
本文详细介绍了 Android 中 Context 类的 getString 方法的用法,通过两个示例展示了如何使用该方法来获取资源文件中的字符串,并进行格式化处理。第一个示例使用了 %s 来代表字符串参数,第二个示例则使用了 %d 来代表整数参数。
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对应字符串

/** * Gets the default rounded corner radius of a display which is determined by the * given display unique id. * * Loads the default dimen{@link R.dimen#rounded_corner_radius} if * {@link R.array#config_displayUniqueIdArray} is not set. * * @hide */ public static int getRoundedCornerRadius(Resources res, String displayUniqueId) { final int index = DisplayUtils.getDisplayUniqueIdConfigIndex(res, displayUniqueId); final TypedArray array = res.obtainTypedArray(R.array.config_roundedCornerRadiusArray); int radius; if (index >= 0 && index < array.length()) { radius = array.getDimensionPixelSize(index, 0); } else { radius = res.getDimensionPixelSize(R.dimen.rounded_corner_radius); String resName = res.getResourceEntryName(R.dimen.rounded_corner_radius); int resId = R.dimen.rounded_corner_radius; String resType = res.getResourceTypeName(resId); String originalValue = res.getString(resId); float dimensionValue = res.getDimension(resId); Slog.d("qjx","resName = "+resName+" resType = "+resType+" originalValue = "+originalValue+" dimensionValue = "+dimensionValue); Slog.d("qjx","radius = "+radius); } array.recycle(); // For devices with round displays (e.g. watches) that don't otherwise provide the rounded // corner radius via resource overlays, we can infer the corner radius directly from the // display size. if (radius == 0 && res.getConfiguration().isScreenRound()) { radius = res.getDisplayMetrics().widthPixels / 2; } return radius; } 怎么在这个方法里调试rounded_corner_radius的值 所以需要手动赋值
最新发布
12-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值