我写了这句话:
String Mess = R.string.mess_1 ;
获取字符串值,但不是返回字符串,而是返回integer类型的id。如何获取它的字符串值?我提到了string.xml文件中的字符串值。
我写了这句话:
String Mess = R.string.mess_1 ;
获取字符串值,但不是返回字符串,而是返回integer类型的id。如何获取它的字符串值?我提到了string.xml文件中的字符串值。
当前回答
在Android中使用getResources()之前必须引用Context名称。
String user=getApplicationContext().getResources().getString(R.string.muser);
OR
Context mcontext=getApplicationContext();
String user=mcontext.getResources().getString(R.string.muser);
其他回答
当你写R.时,你引用的是eclipse创建的R.java类,使用getResources().getString(),并在getString()方法中传递你试图读取的资源的id。
示例:String[] yourStringArray = getResources().getStringArray(R.array.Your_array);
我用这个:
String URL = Resources.getSystem().getString(R.string.mess_1);
你可以使用下面的代码:
getText(R.string.mess_1);
基本上,您需要将资源id作为参数传递给getText()方法。
例如,如果您想将字符串值添加到按钮,简单使用
android:text="@string/NameOfTheString"
在strings.xml中定义的文本是这样的:
<string name="NameOfTheString">Test string</string>
在活动:
this.getString(R.string.resource_name)
如果不在活动中,但可以访问上下文:
context.getString(R.string.resource_name)
application.getString(R.string.resource_name)