我写了这句话:
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)将>保留应用于字符串的任何富文本样式。
其他回答
当你写R.时,你引用的是eclipse创建的R.java类,使用getResources().getString(),并在getString()方法中传递你试图读取的资源的id。
示例:String[] yourStringArray = getResources().getStringArray(R.array.Your_array);
仅供以后参考。
在字符串资源文档中,它说:
您可以使用getString(int)或getText(int)来检索字符串。getText(int)将>保留应用于字符串的任何富文本样式。
getString(R.string.your_string)获取结果
你可以直接读取strings.xml中定义的值:
<resources>
<string name="hello">Hello StackOverflow!</string>
</resources>
并设置为一个变量:
String mymessage = getString(R.string.hello);
但是我们可以在视图中定义字符串:
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"/>
在Android中使用getResources()之前必须引用Context名称。
String user=getApplicationContext().getResources().getString(R.string.muser);
OR
Context mcontext=getApplicationContext();
String user=mcontext.getResources().getString(R.string.muser);