我写了这句话:

String Mess = R.string.mess_1 ;

获取字符串值,但不是返回字符串,而是返回integer类型的id。如何获取它的字符串值?我提到了string.xml文件中的字符串值。


当前回答

当你写R.时,你引用的是eclipse创建的R.java类,使用getResources().getString(),并在getString()方法中传递你试图读取的资源的id。

示例:String[] yourStringArray = getResources().getStringArray(R.array.Your_array);

其他回答

仅供以后参考。

在字符串资源文档中,它说:

您可以使用getString(int)或getText(int)来检索字符串。getText(int)将>保留应用于字符串的任何富文本样式。

我用这个:

String URL = Resources.getSystem().getString(R.string.mess_1);

你可以使用下面的代码:

 getText(R.string.mess_1); 

基本上,您需要将资源id作为参数传递给getText()方法。

试试这个

String mess = getResources().getString(R.string.mess_1);

更新

String string = getString(R.string.hello);

您可以使用getString(int)或getText(int)来检索字符串。getText(int)将保留应用于字符串的任何富文本样式。

参考:https://developer.android.com/guide/topics/resources/string-resource.html

如果你在一个活动中,你可以使用

getResources().getString(R.string.whatever_string_youWant);

如果您不在活动中 用这个:

getApplicationContext.getResource().getString(R.String.Whatever_String_you_want)