鸿蒙如何通过资源Id获取资源的方式(一)

获取Element
/**
     * get the Element
     * @param context 上下文
     * @param resId the id
     * @return the Element
     */
    public static Element getElement(Context context, int resId) {
        Element element = ElementScatter.getInstance(context).parse(resId);
        return element;
    }
获取Color
/**
     * get the color
     *
     * @param context the context
     * @param id      the id
     * @return the color
     */
    public static int getColor(Context context, int id) {
        Log.info(TAG, "Entering getColor");
        int result = 0;
        if (context == null) {
            Log.info(TAG, "getColor -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            Log.info(TAG, "getColor -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getColor();
        } catch (IOException e) {
            Log.info(TAG, "getColor -> IOException");
        } catch (NotExistException e) {
            Log.info(TAG, "getColor -> NotExistException");
        } catch (WrongTypeException e) {
            Log.info(TAG, "getColor -> WrongTypeException");
        }
        return result;
    }
获取Dimen
/**
     * get the dimen value
     *
     * @param context the context
     * @param id      the id
     * @return get the float dimen value
     */
    public static float getDimen(Context context, int id) {
        Log.info(TAG, "Entering getDimen");
        float result = 0;
        if (context == null) {
            Log.info(TAG, "getDimen -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            Log.info(TAG, "getDimen -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getFloat();
        } catch (IOException e) {
            Log.info(TAG, "getDimen -> IOException");
        } catch (NotExistException e) {
            Log.info(TAG, "getDimen -> NotExistException");
        } catch (WrongTypeException e) {
            Log.info(TAG, "getDimen -> WrongTypeException");
        }
        return result;
    }
获取String
 /**
     * get string
     *
     * @param context the context
     * @param id      the string id
     * @return string of the given id
     */
    public static String getString(Context context, int id) {
        Log.info(TAG, "Entering getString");
        String result = "";
        if (context == null) {
            Log.info(TAG, "getString -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            Log.info(TAG, "getString -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getString();
        } catch (IOException e) {
            Log.info(TAG, "getString -> IOException");
        } catch (NotExistException e) {
            Log.info(TAG, "getString -> NotExistException");
        } catch (WrongTypeException e) {
            Log.info(TAG, "getString -> WrongTypeException");
        }
        return result;
    }
获取StringArray
 /**
     * get the string array
     *
     * @param context the context
     * @param id      the string array id
     * @return the string array
     */
    public static String[] getStringArray(Context context, int id) {
        Log.info(TAG, "Entering getStringArray");
        String[] result = null;
        if (context == null) {
            Log.info(TAG, "getStringArray -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            Log.info(TAG, "getStringArray -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getStringArray();
        } catch (IOException e) {
            Log.info(TAG, "getStringArray -> IOException");
        } catch (NotExistException e) {
            Log.info(TAG, "getStringArray -> NotExistException");
        } catch (WrongTypeException e) {
            Log.info(TAG, "getStringArray -> WrongTypeException");
        }
        return result;
    }
获取IntArray
 /**
     * get the int array
     *
     * @param context the context
     * @param id      the int array
     * @return the int array
     */
    public static int[] getIntArray(Context context, int id) {
        Log.info(TAG, "Entering getIntArray");
        int[] result = null;
        if (context == null) {
            Log.info(TAG, "getIntArray -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            Log.info(TAG, "getIntArray -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getIntArray();
        } catch (IOException e) {
            Log.info(TAG, "getIntArray -> IOException");
        } catch (NotExistException e) {
            Log.info(TAG, "getIntArray -> NotExistException");
        } catch (WrongTypeException e) {
            Log.info(TAG, "getIntArray -> WrongTypeException");
        }
        return result;
    }
获取Boolean
/**
     * get boolean
     *
     * @param context the context
     * @param id      the boolean id
     * @return boolean of the given id
     */
    public static boolean getBoolean(Context context, int id) {
        Log.info(TAG, "Entering getBoolean");
        boolean result = false;
        if (context == null) {
            Log.info(TAG, "getBoolean -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            Log.info(TAG, "getBoolean -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getBoolean();
        } catch (IOException e) {
            Log.info(TAG, "getBoolean -> IOException");
        } catch (NotExistException e) {
            Log.info(TAG, "getBoolean -> NotExistException");
        } catch (WrongTypeException e) {
            Log.info(TAG, "getBoolean -> WrongTypeException");
        }
        return result;
    }
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值