如何从android中的string.xml读取值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2183962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to read value from string.xml in android?
提问by UMAR
I have written the line:
我写了一行:
String Mess = R.string.mess_1 ;
to get string value, but instead of returning string, it is giving me id of type integer. How can I get its string value? I mentioned the string value in the string.xml
file.
获取字符串值,但不是返回字符串,而是给我整数类型的 id。我怎样才能得到它的字符串值?我在string.xml
文件中提到了字符串值。
回答by ccheneson
Try this
尝试这个
String mess = getResources().getString(R.string.mess_1);
UPDATE
更新
String string = getString(R.string.hello);
You can use either getString(int)
or getText(int)
to retrieve a string. getText(int)
will retain any rich text styling applied to the string.
您可以使用getString(int)
或getText(int)
来检索字符串。getText(int)
将保留应用于字符串的任何富文本样式。
Reference: https://developer.android.com/guide/topics/resources/string-resource.html
参考:https: //developer.android.com/guide/topics/resources/string-resource.html
回答by Hakan Cakirkan
In Activity:
在活动中:
this.getString(R.string.resource_name)
If not in activity but have access to context:
如果不在活动中但可以访问上下文:
context.getString(R.string.resource_name)
application.getString(R.string.resource_name)
回答by Robertas Uldukis
I'm using this:
我正在使用这个:
String URL = Resources.getSystem().getString(R.string.mess_1);
回答by oabarca
By the way, it is also possible to create string arrays in the strings.xml like so:
顺便说一句,也可以在 strings.xml 中创建字符串数组,如下所示:
<string-array name="tabs_names">
<item>My Tab 1</item>
<item>My Tab 2</item>
</string-array>
And then from your Activity you can get the reference like so:
然后从你的 Activity 你可以得到这样的参考:
String[] tab_names = getResources().getStringArray(R.array.tab_names);
String tabname1=tab_names[0];//"My Tab 1"
回答by Alvaro
Only for future references.
仅供日后参考。
In the String resources documentationit says:
在字符串资源文档中,它说:
You can use either getString(int) or getText(int) to retrieve a string. getText(int) will >retain any rich text styling applied to the string.
您可以使用 getString(int) 或 getText(int) 来检索字符串。getText(int) 将 > 保留应用于字符串的任何富文本样式。
回答by thhVictor
Solution 1
解决方案1
Context context;
String mess = context.getString(R.string.mess_1)
Solution 2
解决方案2
String mess = getString(R.string.mess_1)
回答by ir2pid
In fragments, you can use
在片段中,您可以使用
getActivity().getString(R.id.whatever);
回答by bondus
If you want to add the string value to a button for example, simple use
例如,如果要将字符串值添加到按钮,则简单使用
android:text="@string/NameOfTheString"
The defined text in strings.xmllooks like this:
strings.xml 中定义的文本如下所示:
<string name="NameOfTheString">Test string</string>
回答by Ashish Vora
You must reference Context name before using getResources()
in Android.
getResources()
在 Android 中使用之前,您必须引用上下文名称。
String user=getApplicationContext().getResources().getString(R.string.muser);
OR
或者
Context mcontext=getApplicationContext();
String user=mcontext.getResources().getString(R.string.muser);
回答by coderz
You can use this code:
您可以使用此代码:
getText(R.string.mess_1);
Basically, you need to pass the resource id as a parameter to the getText() method.
基本上,您需要将资源 ID 作为参数传递给 getText() 方法。