1
| public void parseAnnotation(Class<?> clazz) { //传一个类进来
try {
Field[] fields = clazz.getDeclaredFields(); //获取该类的字段
for (Field field : fields) { //遍历字段
if (field.isAnnotationPresent(ViewInject.class)) { //字段被注解标注
ViewInject inject = field.getAnnotation(ViewInject.class); //获取注解对象
int id = inject.value(); //获取注解值
if (id > 0) { //值是正确的
field.setAccessible(true); //该值可达
field.set(this, this.findViewById(id)); //给字段设置值
}
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
|