我写了这句话:

String Mess = R.string.mess_1 ;

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


当前回答

我用这个:

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

其他回答

解决方案1

Context context;
String mess = context.getString(R.string.mess_1)

解决方案2

String mess = getString(R.string.mess_1)

getString(R.string.your_string)获取结果

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

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

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

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

例如,如果您想将字符串值添加到按钮,简单使用

android:text="@string/NameOfTheString"

在strings.xml中定义的文本是这样的:

 <string name="NameOfTheString">Test string</string>

我用这个:

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