属性注解
注解ViewById运行时注解,value就是R.id.xx
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ViewById {
int value();
}
ViewFinder view的帮助类,findviewById
public class ViewFinder {
Activity mActivity;
View mView;
public ViewFinder(Activity activity) {
this.mActivity=activity;
}
public ViewFinder(View view) {
this.mView=view;
}
public View findViewById(int viewId){
return mActivity!=null?mActivity.findViewById(viewId):mView.findViewById(viewId);
}
}
ViewUtils用于与Activity进行直接交互
public class ViewUtils {
public static void inject(Activity activity) {
inject(new ViewFinder(activity), activity);
}
public static void inject(View view) {
inject(new ViewFinder(view), view);
}
public static void inject(View view, Object object) {
inject(new ViewFinder(view), object);
}
private static void inject(ViewFinder finder, Object object) {
injectField(finder, object);
}
//获得注入属性
private static void injectField(ViewFinder finder, Object object) {
//1.获得反射的类
Class<?> clazz = object.getClass();
//2.获得所有属性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
//获得添加注解的属性
ViewById viewById = field.getAnnotation(ViewById.class);
if (viewById != null) {
//获得注解上面的值 R.id,test
int value = viewById.value();
//获得view view->mTextTv
View view = finder.findViewById(value);
if (view != null) {
//私有的共有的都可以
field.setAccessible(true);
try {
//将view设置 mTextTv
field.set(object, view);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
}
事件注解注入
//事件注入
private static void injectEvent(ViewFinder finder, Object object) {
//1.获得所有的方法
Class<?> clazz = object.getClass();
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
OnClick onClick = method.getAnnotation(OnClick.class);
if (onClick != null) {
int[] viewIds = onClick.value();
for (int viewId : viewIds) {
View view = finder.findViewById(viewId);
if (view != null)
view.setOnClickListener(new DeclaredOnClickListener(method, object));
}
}
}
}
private static class DeclaredOnClickListener implements View.OnClickListener {
Method mMethod;
Object mObject;
public DeclaredOnClickListener(Method method, Object object) {
this.mMethod = method;
this.mObject = object;
}
@Override
public void onClick(View v) {
mMethod.setAccessible(true);
try {
mMethod.invoke(mObject, v);
} catch (Exception e) {
e.printStackTrace();
try {
mMethod.invoke(mObject, null);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
自动检测网络扩展
CheckNet注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckNet {
}
修改事件的注入
//事件注入
private static void injectEvent(ViewFinder finder, Object object) {
//1.获得所有的方法
Class<?> clazz = object.getClass();
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
OnClick onClick = method.getAnnotation(OnClick.class);
if (onClick != null) {
int[] viewIds = onClick.value();
for (int viewId : viewIds) {
View view = finder.findViewById(viewId);
//扩展判断是否有网
boolean isCheckNet = method.getAnnotation(CheckNet.class) != null;
if (view != null) {
view.setOnClickListener(new DeclaredOnClickListener(method, object, isCheckNet));
}
}
}
}
}
private static class DeclaredOnClickListener implements View.OnClickListener {
Method mMethod;
Object mObject;
boolean mIsCheckNet;
public DeclaredOnClickListener(Method method, Object object, boolean isCheckNet) {
this.mMethod = method;
this.mObject = object;
this.mIsCheckNet = isCheckNet;
}
@Override
public void onClick(View v) {
// 需不需要检测网络
if (mIsCheckNet) {
// 需要
if (!networkAvailable(v.getContext())) {
Toast.makeText(v.getContext(), "亲,您的网络不太给力", Toast.LENGTH_LONG).show();
return;
}
}
try {
mMethod.setAccessible(true);
mMethod.invoke(mObject, v);
} catch (Exception e) {
e.printStackTrace();
try {
mMethod.invoke(mObject, null);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
/**
* 判断当前网络是否可用
*/
private static boolean networkAvailable(Context context) {
// 得到连接管理器对象
try {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}