Android - Loop Through strings.xml文件 [英] Android - Loop Through strings.xml file

查看:160
本文介绍了Android - Loop Through strings.xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否还有循环遍历 strings.xml 文件。

I was wondering if there is anyway to loop through the strings.xml file.

让我们说我有以下格式:

Let's say that I have the following format:

<!-- FIRST SECTION -->
<string name="change_password">Change Password</string>
<string name="change_server">Change URL</string>
<string name="default_password">password</string>
<string name="default_server">http://xxx:8080</string>
<string name="default_username">testPhoneAccount</string>

<!-- SECOND SECTION -->
<string name="debug_settings_category">Debug Settings</string>
<string name="reload_data_every_startup_pref">reload_data_every_startup</string>
<string name="reload_data_on_first_startup_pref">reload_data_on_first_startup</string>

现在让我说我有这个:

private HashMap<String,Integer> hashmapStringValues = new HashMap<String, Integer>();

有没有办法只在我的xml文件的第二部分进行迭代?也许用< section2> 之类的标签包装该部分,然后遍历它?

Is there a way to iterate only in the second section of my xml file? Maybe wrap the section with a tag like <section2> and then iterate through it?

public void initHashMap(){
    for (int i=0;i< ???? ;i++) //Here I need to loop only in the second section of my xml file
    {          
        String nameOfTag = ? // Here I get the name of the tag
        int value = R.string.nameOfTag // Here I get the associated value of the tag

        this.hashmapStringValues.put(nameOfTag,value);
    }
}


推荐答案

否,但您可以在资源/值中创建另一个包含以下内容的xml文件:

No, but you can create another xml file in resources/values which contains this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="array">
        <item>ID_1|Asdf</item>
        <item>ID_2|I do not like to think</item>
        <item>ID_3|Whatever</item>
    </string-array>

</resources>

您可以根据需要格式化字符串,然后使用自定义解析器解析它们。

You can format your strings however you like, and then parse them with a custom parser.

要获得字符串数组,您只需要这样做:

To get the string array you only need to do this:

getResources().getStringArray(R.array.array);

这篇关于Android - Loop Through strings.xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆