Show string keys instead of values

只愿长相守 提交于 2019-12-11 19:01:21

问题


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.

for example

<string name="jobs_key">Jobs</string>

i would like to show in the UI jobs_key instead of Jobs


回答1:


use 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;
    }

and call it like getKeysName(context, "string");, to get, for instance all the keys declared inside string.xml.



来源:https://stackoverflow.com/questions/24060574/show-string-keys-instead-of-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!