如何从strings.xml中的字符串数组中获取字符串

时间:2015-07-01 10:50:12

标签: android android-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);

2 个答案:

答案 0 :(得分:8)

要从strings.xml获取字符串,你应该为key获得String。但是在上面你有String数组的键,所以你必须得到你需要的字符串数组得到必需的字符串。

CurrentWebpage = getResources().getStringArray(R.array.link_categories)[0];

希望这会对你有所帮助。

答案 1 :(得分:1)

您需要在下面写下代码: -

Context con=getApplicationContext();
String[] your_array = con.getResources().getStringArray(R.array.your_array_string);

现在你有了一个字符串数组,你可以从你的数组中选择你的字符串。

WebPageCurrent = your_array[0];// may be [1],[2]