how to read value from string.xml in android?
我写了这样的话:
1 | String Mess = R.string.mess_1 ; |
获取字符串值,但不是返回字符串,而是给我id类型的整数。 如何获取其字符串值? 我在
尝试这个
1 | String mess = getResources().getString(R.string.mess_1); |
更新
1 | String string = getString(R.string.hello); |
您可以使用
参考:https://developer.android.com/guide/topics/resources/string-resource.html
活动中:
1 | this.getString(R.string.resource_name) |
如果不在活动中但可以访问上下文:
1 2 | context.getString(R.string.resource_name) application.getString(R.string.resource_name) |
我正在使用这个:
1 | String URL = Resources.getSystem().getString(R.string.mess_1); |
顺便说一句,也可以像这样在strings.xml中创建字符串数组:
1 2 3 4 | <string-array name="tabs_names"> <item>My Tab 1</item> <item>My Tab 2</item> </string-array> |
然后从"活动"中获取像这样的参考:
1 2 | String[] tab_names = getResources().getStringArray(R.array.tab_names); String tabname1=tab_names[0];//"My Tab 1" |
仅供以后参考。
在字符串资源文档中说:
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.
解决方案1
1 2 | Context context; String mess = context.getString(R.string.mess_1) |
解决方案2
1 | String mess = getString(R.string.mess_1) |
例如,如果要将字符串值添加到按钮,则使用简单
1 | android:text="@string/NameOfTheString" |
strings.xml中定义的文本如下所示:
1 | <string name="NameOfTheString">Test string</string> |
在Android中使用
1 | String user=getApplicationContext().getResources().getString(R.string.muser); |
要么
1 2 3 | Context mcontext=getApplicationContext(); String user=mcontext.getResources().getString(R.string.muser); |
在片段中,您可以使用
1 | getActivity().getString(R.id.whatever); |
您可以使用以下代码:
1 | getText(R.string.mess_1); |
Basically, you need to pass the resource id as a parameter to the getText() method.
如果您正在参加活动,则可以使用
1 | getResources().getString(R.string.whatever_string_youWant); |
如果您不在活动中
用这个 :
1 | getApplicationContext.getResource().getString(R.String.Whatever_String_you_want) |
wmh
现在,您的字符串已复制到myString中。希望它对您有用。
**
I hope this code is beneficial
**
1 | String user = getResources().getString(R.string.muser); |
更新资料
-
您可以在
Activity 或Fragment 中使用getString(R.string.some_string_id) 。 -
您可以在没有直接访问
getString() 方法的地方使用Context.getString(R.string.some_string_id) 。就像Dialog 。
问题是您没有
假设下面的方法没有上下文。
1 2 3 4 | public void someMethod(){ ... // can't use getResource() or getString() without Context. } |
现在,您将在此方法中将
1 2 3 4 | public void someMethod(Context context){ ... context.getString(R.string.some_id); } |
我要做的是
1 2 3 4 | public void someMethod(){ ... App.getRes().getString(R.string.some_id) } |
什么?在您的应用程序中的任何地方使用非常简单!
因此,这里有一个独特的Bonus奖励解决方案,您可以通过该解决方案从
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import android.app.Application; import android.content.res.Resources; public class App extends Application { private static App mInstance; private static Resources res; @Override public void onCreate() { super.onCreate(); mInstance = this; res = getResources(); } public static App getInstance() { return mInstance; } public static Resources getResourses() { return res; } } |
将名称字段添加到您的
Now you are good to go.
wmh
wmhDetails
- Android Studio 3.1.4
- Kotlin version: 1.2.60
Task
- single line use
- minimum code
- use suggestions from the compiler
Step 1. Application()
Get link to the context of you application
Step 2. Add int extension
Usage
strings.xml
Get string value:
Results

wmh
wmh
getString(R.string.your_string) get the result
wmh
当您写
示例: