0

我在元素文件夹下的 string.json 中定义了一个字符串名称和值。如何在 java 代码中访问这些值?

4

1 回答 1

0

使用以下代码段从 strings.json 访问 String

 public static String getString(Context context, int id) {
        String result = "";
        if (context == null) {
            LogUtil.error(TAG, "getString -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            LogUtil.error(TAG, "getString -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getString();
        } catch (IOException e) {
            LogUtil.error(TAG, "getString -> IOException");
        } catch (NotExistException e) {
            LogUtil.error(TAG, "getString -> NotExistException");
        } catch (WrongTypeException e) {
            LogUtil.error(TAG, "getString -> WrongTypeException");
        }
        return result;
    }

和用法将是,

String appName = getString(context, ResourceTable.String_appname);

于 2021-07-30T09:01:54.933 回答