如何从strings.xml中动态地访问值? [英] How to access the values from strings.xml dynamically?
问题描述
我想要做的是动态获取从的strings.xml
特定的文本。我认为这会涉及动态地访问对象变量。
会有像一个函数:
What I want to do is to get a specific text from strings.xml
dynamically. I think it will involve to access an object variable dynamically.
There will be a function like:
public void getDynamicString(int level) {
text.setText(R.string.levelText_+level);
}
而在的strings.xml
会有<字符串名称=levelText_5>文字水平5℃/串>
我宁愿不创建的所有文字资源的列表。我们可以用Java / Android的做到这一点。
I would rather not create a list with all the text resources. Can one do this in Java/Android.
推荐答案
使用方法则getIdentifier(姓名,DEFTYPE,defPackage)
的资源类来获得的id资源的名字。然后,你可以做一个正常的的getString(ID)
从同一类。
Use the method getIdentifier(name, defType, defPackage)
of the Resources class to get the id of a resource by name. Then you can do a normal getString(id)
from the same class.
修改:有点谷歌搜索发现这一点:这个。你可以找到样本的使用存在。
EDIT: a bit of Googling revealed this: this. You can find sample usage there.
这篇关于如何从strings.xml中动态地访问值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!