显示字符串键而不是值

编程入门 行业动态 更新时间:2024-10-28 12:17:26
本文介绍了显示字符串键而不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以在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.

更多推荐

显示字符串键而不是值

本文发布于:2023-11-05 19:52:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   而不是

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!