How do you access the values in the res/values/string.xml
resource file from the Android Activity class
?
如何从Android Activity类访问res / values / string.xml资源文件中的值?
5 个解决方案
#1
127
Well you can get String using,
那么你可以使用String,
getString(R.string.app_name);
And, you can get string-array using
并且,您可以使用字符串数组
String arr[] = getResources().getStringArray(R.array.planet);
for (int i = 0; i < arr.length; i++) {
Toast.makeText(getBaseContext(),arr[i], Toast.LENGTH_LONG).show();
}
#2
39
strings.xml:
strings.xml中:
<string name="some_text">Some Text</string>
Activity:
活动:
getString(R.string.some_text);
#3
13
If getString(R.string.app_name); isn't working for you, you can pass context like this:
如果是getString(R.string.app_name);不适合你,你可以传递这样的上下文:
context.getString(R.string.app_name);
#4
12
Put this code in res/values/string.xml
将此代码放在res / values / string.xml中
<string-array name="planet">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
This code to be placed in res/layout/main.xml
and remove default widgets present in main.xml
.
此代码放在res / layout / main.xml中,并删除main.xml中的默认小部件。
<ListView android:id="@+id/planet"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:entries="@array/planet"/>
</LinearLayout>
#5
0
If you have the context of Activity, Go with
如果您有活动的上下文,请使用
getString(R.string.app_name);
的getString(R.string.app_name);
If you dont have context, Try below.
如果您没有上下文,请尝试以下操作。
you can get the context from activity using Constructor.
您可以使用Constructor从活动中获取上下文。
mContext.getString(R.string.app_name);
mContext.getString(R.string.app_name);
#1
127
Well you can get String using,
那么你可以使用String,
getString(R.string.app_name);
And, you can get string-array using
并且,您可以使用字符串数组
String arr[] = getResources().getStringArray(R.array.planet);
for (int i = 0; i < arr.length; i++) {
Toast.makeText(getBaseContext(),arr[i], Toast.LENGTH_LONG).show();
}
#2
39
strings.xml:
strings.xml中:
<string name="some_text">Some Text</string>
Activity:
活动:
getString(R.string.some_text);
#3
13
If getString(R.string.app_name); isn't working for you, you can pass context like this:
如果是getString(R.string.app_name);不适合你,你可以传递这样的上下文:
context.getString(R.string.app_name);
#4
12
Put this code in res/values/string.xml
将此代码放在res / values / string.xml中
<string-array name="planet">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
This code to be placed in res/layout/main.xml
and remove default widgets present in main.xml
.
此代码放在res / layout / main.xml中,并删除main.xml中的默认小部件。
<ListView android:id="@+id/planet"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:entries="@array/planet"/>
</LinearLayout>
#5
0
If you have the context of Activity, Go with
如果您有活动的上下文,请使用
getString(R.string.app_name);
的getString(R.string.app_name);
If you dont have context, Try below.
如果您没有上下文,请尝试以下操作。
you can get the context from activity using Constructor.
您可以使用Constructor从活动中获取上下文。
mContext.getString(R.string.app_name);
mContext.getString(R.string.app_name);