Android读取资源文件夹里的string-array字符串数组

在Android中,某些可能是读取一大串资源文件夹里面的数组,图片,颜色等等这样的需求。
于是我制作了一个这样的工具类,方便了操作与读取!
下面例子:

public class KdType {

    private static String[] name;
    private static String[] code;
    private static Resources res;
    private static KdType kdType = null;

    private KdType(Context context) {
        res = context.getResources();
        name = res.getStringArray(R.array.kd_name);
        code = res.getStringArray(R.array.kd_code);
    }

    /**
     * 初始化一次即可
     * 程序开始时初始化
     * @param context
     * @return
     */
    public static KdType initType(Context context) {
        if (kdType == null) {

            synchronized (KdType.class) {
                if (kdType == null) {
                    kdType = new KdType(context);
                }

            }
        }
        return kdType;
    }

    /**
     * 返回名称数组
     * @return
     */
    public static String[] getNameArray() {
        return name;
    }

    /**
     * 返回代号数组
     * @return
     */
    public static String[] getCodeArray() {
        return code;
    }

    /**
     * 返回名称集合
     * @return
     */
    public static List getNameList() {
        List mlist = new ArrayList<>();
        Collections.addAll(mlist, name);
        return mlist;
    }

    /**
     * 返回代号集合
     * @return
     */
    public static List getCodeList(){
        List mlist = new ArrayList<>();
        Collections.addAll(mlist,code);
        return mlist;
    }
}

下面是部分string-array示例:

 

        顺丰速运
        百世快递
        中通快递
        申通快递
        圆通速递 
    

    
      SF
        HTKY
        ZTO
        STO
        YTO
    

也可以读取颜色,比如要一个随机颜色等等
例子:

public class TypeColor {
    private Context mContext;
    private static TypeColor typeColor;
    private static Resources res;
    private static int[] colorArray;//图片资源文件数组

    private TypeColor(Context context) {
        mContext = context;
        res = context.getResources();
        colorArray = res.getIntArray(R.array.color);

    }

    //程序启动时初始化
    public static TypeColor initType(Context context) {
         synchronized (KdType.class) {
	        if (typeColor == null) {
	            return new TypeColor(context);
	        }
       	 }
        return typeColor;
    }

    private static Random random = new Random();

	/**
	*获取随机颜色代码
	*/
    public static int getRandomColor() {
        return colorArray[random.nextInt(getLength())];
    }

    public static int getLength() {
        return colorArray.length;
    }

}


        #f6e58d
        #ffbe76
        #ff7979
        #badc58
        #f9ca24
        #f0932b
        #eb4d4b
        #6ab04c
        #7ed6df
        #e056fd
        #686de0
        #22a6b3
        #f3a683
        #f7d794
        #778beb
        #e77f67
        #cf6a87
        #f19066
        #f5cd79
        #546de5
        #e15f41
        #c44569
        #786fa6
        #f8a5c2
        #63cdda
        #ea8685
        #596275
        #f78fb3
        #3dc1d3
    

还可读取图片,里面存放的是 “R.drawable.icon_1"这样的型式,这里就不举例。

你可能感兴趣的:(Android工具类,Android开发)