显示字符串键而不是值 [英] Show string keys instead of values

查看:88
本文介绍了显示字符串键而不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于显示字符串键而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆