Android – Loop Through strings.xml文件

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

假设我有以下格式:

  Change Password Change URL password http://xxx:8080 testPhoneAccount  Debug Settings reload_data_every_startup reload_data_on_first_startup 

现在让我说我有这个:

 private HashMap hashmapStringValues = new HashMap(); 

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

 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文件:

    ID_1|Asdf ID_2|I do not like to think ID_3|Whatever   

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

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

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

如果您查看生成的android.R java文件,它会让您知道如何通过Reflection实现此目的:

  Field fields[] = R.string.class.getFields(); for (Field field : fields) { Log.d("appname", field.getName() + ":" + getResources().getIdentifier(field.getName(), "string", this.getPackageName())); } 

注意,我不会经常这样做,但一旦App加载应该没问题。

这是不可能的,因为基本的java本身没有检测到注释

你只能选择这样的字符串。

 getResources().getString(R.string.app_name)