如何从 strings.xml 中的字符串数组获取字符串
How to get string from string-array in strings.xml
是否可以从 xml 文件中的字符串数组中获取字符串?
我需要一个字符串而不是字符串数组:)
strings.xml
<string-array name="link_categories">
<item>http://google.com</item>
<item>http://yahoo.com</item>
</string-array>
Brabra.java
WebPageCurrent = getResources().getString(get string from string array);
您需要编写以下代码:-
Context con=getApplicationContext();
String[] your_array = con.getResources().getStringArray(R.array.your_array_string);
现在你有了字符串数组,你可以从数组中选择你的字符串。
WebPageCurrent = your_array[0];// may be [1],[2]
要从 strings.xml
获取字符串,您应该为 String
设置 key
。但是在上面,您有字符串数组的键,因此您必须从中获取字符串数组必须获得所需的字符串。
CurrentWebpage = getResources().getStringArray(R.array.link_categories)[0];
希望对您有所帮助。
是否可以从 xml 文件中的字符串数组中获取字符串? 我需要一个字符串而不是字符串数组:)
strings.xml
<string-array name="link_categories">
<item>http://google.com</item>
<item>http://yahoo.com</item>
</string-array>
Brabra.java
WebPageCurrent = getResources().getString(get string from string array);
您需要编写以下代码:-
Context con=getApplicationContext();
String[] your_array = con.getResources().getStringArray(R.array.your_array_string);
现在你有了字符串数组,你可以从数组中选择你的字符串。
WebPageCurrent = your_array[0];// may be [1],[2]
要从 strings.xml
获取字符串,您应该为 String
设置 key
。但是在上面,您有字符串数组的键,因此您必须从中获取字符串数组必须获得所需的字符串。
CurrentWebpage = getResources().getStringArray(R.array.link_categories)[0];
希望对您有所帮助。