假设有这个字符串:
你的玩家有一个不错的门将技能和一个相当好的人际技能
现在字符串中未加粗的部分始终相同,已知运行时加粗的部分。
那么如何实现以下操作:
你的玩家有一个{var1}门将技能和一个{var2}人际技能
然后在运行时填入正确的值来填充这些变量?
我不想像这样连接字符串:
"You player has a" + var1 + "keeper skill and a" + var2 + "people skill"
假设有这个字符串:
你的玩家有一个不错的门将技能和一个相当好的人际技能
现在字符串中未加粗的部分始终相同,已知运行时加粗的部分。
那么如何实现以下操作:
你的玩家有一个{var1}门将技能和一个{var2}人际技能
然后在运行时填入正确的值来填充这些变量?
我不想像这样连接字符串:
"You player has a" + var1 + "keeper skill and a" + var2 + "people skill"
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
</resources>
之后在代码中,您可以替换为
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);
在 Strings.xml 文件中
You player has a %1$d keeper skill and a %2$d people skill
在Java中
getString(R.string.article_stats, var1, var2);
是的,请查看Android开发指南中的以下内容:
如果您需要使用String.format(String, Object...)
格式化字符串,则可以将格式化参数放入字符串资源中。例如,对于以下资源:
<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);
<![CDATA[<b> %1$s bought </b>, last purchased from %2$s <b> %3$s </b>]]>
在你的Java类中
String detail = String.format(getString(R.string.detail),15,"New Delhi","23 mins ago");
detailView.setText(Html.fromHtml(detail ));
Resources.getString(resId, ...)
或Context.getString(resId, ...)
与类似于"你的玩家有 %1$s 的守门技能和 %2$s 的人际技能"
的字符串是你要找的吗? - Salw