<string id="I want To Get This">and this</string>
<string id="I want To Get This">and this</string>
<string id="I want To Get This">and this</string>
如何获得价值
要从字符串资源中获取值,请使用
String stringToCompare= getString(R.string.my_custom_id);
之后,您可以将该值添加到列表中
myList.add(value)
现在,如果你说所有相似,我想你的意思是相等的值或至少相同的字符。为此,您可以通过反射迭代所有字符串值
for (Field field : R.string.class.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()) && !Modifier.isPrivate(field.getModifiers()) && field.getType().equals(int.class)) {
try {
if (field.getName().toLoweCase().equals(stringToCompare.toLowerCase())) {
int id = field.getInt(null);
myList.add(getString(id))
}
} catch (IllegalArgumentException e) {
// ignore
} catch (IllegalAccessException e) {
// ignore
}
}
}
或者可以在这里找到另一种进行迭代的方法;字符串.xml";资源文件?