Back to project page FrameLite.
The source code is released under:
GNU General Public License
If you think the Android project FrameLite listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.miku.framelite.utils; //from ww w. j a va2 s . c om import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.List; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import com.miku.framelite.annotations.ViewInject; /** * ??View???? * * @author xr.lee * */ public class ViewUtils { /** * * @param instance * currentIntance of Using this method ??????????????? * @param endClazz * ?????????View??????????? * @param view * ?????view???????id,???????????instance???instance???activity????? * @param listener * ?????? */ public static void bindViewIds(Object instance, Class<?> endClazz, View parentView, OnClickListener listener) { List<Field> fields = new ArrayList<Field>(); Class<?> clazz = instance.getClass(); while (!clazz.equals(endClazz)) { Field[] fs = clazz.getDeclaredFields(); if (fs != null && fs.length > 0) { Collections.addAll(fields, fs); } clazz = clazz.getSuperclass(); } Object obj2Search = parentView == null ? instance : parentView; for (Field field : fields) { try { field.setAccessible(true); if (field.get(instance) != null) continue; ViewInject viewInject = field.getAnnotation(ViewInject.class); if (viewInject != null) { int viewId = viewInject.id(); View tempView = null; if (obj2Search instanceof View) { tempView = ((View) obj2Search).findViewById(viewId); } else if (obj2Search instanceof Activity) { tempView = ((Activity) obj2Search).findViewById(viewId); } field.set(instance, tempView); if (listener != null && viewInject.bindClick()) { Object obj = field.get(instance); if (obj instanceof View) { ((View) obj).setOnClickListener(listener); } } } } catch (Exception e) { e.printStackTrace(); } } } }