我写了这句话:
String Mess = R.string.mess_1 ;
获取字符串值,但不是返回字符串,而是返回integer类型的id。如何获取它的字符串值?我提到了string.xml文件中的字符串值。
我写了这句话:
String Mess = R.string.mess_1 ;
获取字符串值,但不是返回字符串,而是返回integer类型的id。如何获取它的字符串值?我提到了string.xml文件中的字符串值。
当前回答
仅供以后参考。
在字符串资源文档中,它说:
您可以使用getString(int)或getText(int)来检索字符串。getText(int)将>保留应用于字符串的任何富文本样式。
其他回答
解决方案1
Context context;
String mess = context.getString(R.string.mess_1)
解决方案2
String mess = getString(R.string.mess_1)
在活动:
this.getString(R.string.resource_name)
如果不在活动中,但可以访问上下文:
context.getString(R.string.resource_name)
application.getString(R.string.resource_name)
仅供以后参考。
在字符串资源文档中,它说:
您可以使用getString(int)或getText(int)来检索字符串。getText(int)将>保留应用于字符串的任何富文本样式。
**
我希望这个代码是有益的
**
String user = getResources().getString(R.string.muser);
如果你在一个活动中,你可以使用
getResources().getString(R.string.whatever_string_youWant);
如果您不在活动中 用这个:
getApplicationContext.getResource().getString(R.String.Whatever_String_you_want)