本文介绍了显示字符串键而不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在strings.xml中显示我的字符串的键而不是值,可以很直接地在UI中检查哪个键.
Is it possible to show the keys of my strings in strings.xml instead of the value, would be cool to check which key is it directly in the UI.
例如
<string name="jobs_key">Jobs</string>我想在用户界面jobs_key中显示,而不是Jobs
i would like to show in the UI jobs_key instead of Jobs
推荐答案使用Resources.getResourcesName(int),
返回给定资源标识符的全名
Return the full name for a given resource identifier
此处,您可以找到文档.您还可以使用反射:
here you can find the documentation. You can also use reflection:
private ArrayList<String> getKeysName(Context context, String className) { Class c; ArrayList<String> fieldsName = new ArrayList<String>(); try { c = Class.forName(context.getPackageName() + ".R$" + className); Field[] fields = c.getDeclaredFields(); for (Field f : fields) { Log.e("LOG_TAG", " " + f.getName()); fieldsName.add(f.getName()); } } catch (ClassNotFoundException e) { e.printStackTrace(); } return fieldsName; }并像getKeysName(context, "string");一样调用它,以获取例如在string.xml中声明的所有键.
and call it like getKeysName(context, "string");, to get, for instance all the keys declared inside string.xml.
更多推荐
显示字符串键而不是值
发布评论