码迷,mamicode.com
首页 > 移动开发 > 详细

反射android R文件

时间:2014-12-31 08:46:18      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

动态取得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值
public CharSequence[] getListData(String name) {
        try {
            Class<?> arrayClass = getArrayResource().getClass();
            Field intField = arrayClass.getField(name);
            int sourceId = intField.getInt(name);
            return getResources().getTextArray(sourceId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
private Object arrayResource = null;
 
private Object getArrayResource() {
        if (arrayResource == null) {
            Class<?> resource = R.class;
            try {
                Class<?>[] classes = resource.getClasses();
                for (Class<?> c : classes) {
                    int i = c.getModifiers();
                    String className = c.getName();
                    String s = Modifier.toString(i);
                    if (s.contains("static") && className.contains("array")) {
                        return c.getConstructor().newInstance();
                    } else {
                        continue;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return arrayResource;
    }

反射android R文件

标签:

原文地址:http://blog.csdn.net/u014311064/article/details/42286667

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!