1、String 占位
<string name="welcome_messages">Hello, %2$s! You have %1$s %1$s new messages.</string>
String textStr = getResources().getString(R.string.welcome_messages, "我是", "小明");
text.setText(textStr);
结果:Hello, 小明 ! You have 我是 我是 new messages.
<string name="welcome_messages">Hello, %s! You have %s s new messages.%s</string>
String textStr = getResources().getString(R.string.welcome_messages, "我是", "小明","明天");
text.setText(textStr);
结果:Hello, 小明 ! You have 我是 new messages .明天
getText(int resId)方法和getString(int resId)、getResources().getString(int resId,"","")方法的区别
getText:返回CharSequence
getString:内部getResources().getString
如果字符串是html样式
String text = "<b>加粗字体</b>"正常显示
String text = getString(R.string.welcome);//会自动去掉了标签,需要手动转义,否则显示不正常
本文详细介绍了在Android开发中如何使用String资源文件进行占位符的设置与解析,通过实例展示了不同参数数量下getString方法的应用,以及getText与getString方法的区别。同时,文章还提到了当字符串包含HTML样式时的处理方式。
784

被折叠的 条评论
为什么被折叠?



