字符串资源是否可以使用“化合物标识符”? 像这样
val level = 1 <- current level
getString(R.string.answer + level) <- and this is get (R.string.answer1) string
有可能吗?
答案 0 :(得分:1)
利用Resources.getIdentifier(String, String, String)
方法动态生成所需的资源ID,然后使用该生成的ID正常调用getString(int)
。
例如
int level = 1;
String name = "answer" + level;
int stringResId = getResources().getIdentifier(name, "string", getPackageName());
String result = getString(stringResId);
有关参数的Javadoc信息,请参见链接。
有关更多详细信息,请参阅相关的重复文章,包括Kotlin示例: