当前位置: 代码迷 >> Android >> 反照android R文件
  详细解决方案

反照android R文件

热度:112   发布时间:2016-04-28 03:05:30.0
反射android R文件

动态取得R文件的int值

[1].[代码] [Java]代码 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//通过string-array name属性动态获取R.array.xxx的int值
publicCharSequence[] getListData(String name) {
        try{
            Class<?> arrayClass = getArrayResource().getClass();
            Field intField = arrayClass.getField(name);
            intsourceId = intField.getInt(name);
            returngetResources().getTextArray(sourceId);
        }catch(Exception e) {
            e.printStackTrace();
        }
        returnnull;
    }
 
privateObject arrayResource = null;
 
privateObject getArrayResource() {
        if(arrayResource == null) {
            Class<?> resource = R.class;
            try{
                Class<?>[] classes = resource.getClasses();
                for(Class<?> c : classes) {
                    inti = c.getModifiers();
                    String className = c.getName();
                    String s = Modifier.toString(i);
                    if(s.contains("static") && className.contains("array")) {
                        returnc.getConstructor().newInstance();
                    }else{
                        continue;
                    }
                }
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
        returnarrayResource;
    }

  相关解决方案