@ContentView(value = R.layout.activity_main)
public class MainActivity extends BaseActivity
{
@ViewInject(R.id.id_btn)
private Button mBtn1;
@ViewInject(R.id.id_btn02)
private Button mBtn2;
package com.zhy.ioc.view.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ContentView
{
int value();
}
@ContentView(value = R.layout.activity_main)
public class MainActivity
package com.zhy.ioc.view.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ViewInject
{
int value();
}
@ViewInject(R.id.id_btn) private Button mBtn1;
public enum ElementType {
/**
* Class, interface or enum declaration.
*/
TYPE,
/**
* Field declaration.
*/
FIELD,
/**
* Method declaration.
*/
METHOD,
/**
* Parameter declaration.
*/
PARAMETER,
/**
* Constructor declaration.
*/
CONSTRUCTOR,
/**
* Local variable declaration.
*/
LOCAL_VARIABLE,
/**
* Annotation type declaration.
*/
ANNOTATION_TYPE,
/**
* Package declaration.
*/
PACKAGE
}
public enum RetentionPolicy {
/**
* Annotation is only available in the source code.
*/
SOURCE,
/**
* Annotation is available in the source code and in the class file, but not
* at runtime. This is the default policy.
*/
CLASS,
/**
* Annotation is available in the source code, the class file and is
* available at runtime.
*/
RUNTIME
}
package com.zhy.zhy_xutils_test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.zhy.ioc.view.ViewInjectUtils;
import com.zhy.ioc.view.annotation.ContentView;
import com.zhy.ioc.view.annotation.ViewInject;
@ContentView(value = R.layout.activity_main)
public class MainActivity extends Activity implements OnClickListener
{
@ViewInject(R.id.id_btn)
private Button mBtn1;
@ViewInject(R.id.id_btn02)
private Button mBtn2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ViewInjectUtils.inject(this);
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.id_btn:
Toast.makeText(MainActivity.this, "Why do you click me ?",
Toast.LENGTH_SHORT).show();
break;
case R.id.id_btn02:
Toast.makeText(MainActivity.this, "I am sleeping !!!",
Toast.LENGTH_SHORT).show();
break;
}
}
}
/**
* 注入主布局文件
*
* @param activity
*/
private static void injectContentView(Activity activity)
{
Class<? extends Activity> clazz = activity.getClass();
// 查询类上是否存在ContentView注解
ContentView contentView = clazz.getAnnotation(ContentView.class);
if (contentView != null)// 存在
{
int contentViewLayoutId = contentView.value();
try
{
Method method = clazz.getMethod(METHOD_SET_CONTENTVIEW,
int.class);
method.setAccessible(true);
method.invoke(activity, contentViewLayoutId);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
private static final String METHOD_SET_CONTENTVIEW = "setContentView";
private static final String METHOD_FIND_VIEW_BY_ID = "findViewById";
/**
* 注入所有的控件
*
* @param activity
*/
private static void injectViews(Activity activity)
{
Class<? extends Activity> clazz = activity.getClass();
Field[] fields = clazz.getDeclaredFields();
// 遍历所有成员变量
for (Field field : fields)
{
ViewInject viewInjectAnnotation = field
.getAnnotation(ViewInject.class);
if (viewInjectAnnotation != null)
{
int viewId = viewInjectAnnotation.value();
if (viewId != -1)
{
Log.e("TAG", viewId+"");
// 初始化View
try
{
Method method = clazz.getMethod(METHOD_FIND_VIEW_BY_ID,
int.class);
Object resView = method.invoke(activity, viewId);
field.setAccessible(true);
field.set(activity, resView);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
public static void inject(Activity activity)
{
injectContentView(activity);
injectViews(activity);
}
package com.zhy.zhy_xutils_test;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.zhy.ioc.view.annotation.ContentView;
import com.zhy.ioc.view.annotation.OnClick;
import com.zhy.ioc.view.annotation.ViewInject;
@ContentView(value = R.layout.activity_main)
public class MainActivity extends BaseActivity
{
@ViewInject(R.id.id_btn)
private Button mBtn1;
@ViewInject(R.id.id_btn02)
private Button mBtn2;
@OnClick({ R.id.id_btn, R.id.id_btn02 })
public void clickBtnInvoked(View view)
{
switch (view.getId())
{
case R.id.id_btn:
Toast.makeText(this, "Inject Btn01 !", Toast.LENGTH_SHORT).show();
break;
case R.id.id_btn02:
Toast.makeText(this, "Inject Btn02 !", Toast.LENGTH_SHORT).show();
break;
}
}
}
package com.zhy.ioc.view.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventBase
{
Class<?> listenerType();
String listenerSetter();
String methodName();
}
package com.zhy.ioc.view.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import android.view.View;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@EventBase(listenerType = View.OnClickListener.class, listenerSetter = "setOnClickListener", methodName = "onClick")
public @interface OnClick
{
int[] value();
}
@OnClick({ R.id.id_btn, R.id.id_btn02 })
public void clickBtnInvoked(View view)
public static void inject(Activity activity)
{
injectContentView(activity);
injectViews(activity);
injectEvents(activity);
}
/**
* 注入所有的事件
*
* @param activity
*/
private static void injectEvents(Activity activity)
{
Class<? extends Activity> clazz = activity.getClass();
Method[] methods = clazz.getMethods();
//遍历所有的方法
for (Method method : methods)
{
Annotation[] annotations = method.getAnnotations();
//拿到方法上的所有的注解
for (Annotation annotation : annotations)
{
Class<? extends Annotation> annotationType = annotation
.annotationType();
//拿到注解上的注解
EventBase eventBaseAnnotation = annotationType
.getAnnotation(EventBase.class);
//如果设置为EventBase
if (eventBaseAnnotation != null)
{
//取出设置监听器的名称,监听器的类型,调用的方法名
String listenerSetter = eventBaseAnnotation
.listenerSetter();
Class<?> listenerType = eventBaseAnnotation.listenerType();
String methodName = eventBaseAnnotation.methodName();
try
{
//拿到Onclick注解中的value方法
Method aMethod = annotationType
.getDeclaredMethod("value");
//取出所有的viewId
int[] viewIds = (int[]) aMethod
.invoke(annotation, null);
//通过InvocationHandler设置代理
DynamicHandler handler = new DynamicHandler(activity);
handler.addMethod(methodName, method);
Object listener = Proxy.newProxyInstance(
listenerType.getClassLoader(),
new Class<?>[] { listenerType }, handler);
//遍历所有的View,设置事件
for (int viewId : viewIds)
{
View view = activity.findViewById(viewId);
Method setEventListenerMethod = view.getClass()
.getMethod(listenerSetter, listenerType);
setEventListenerMethod.invoke(view, listener);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
package com.zhy.ioc.view;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashMap;
public class DynamicHandler implements InvocationHandler
{
private WeakReference<Object> handlerRef;
private final HashMap<String, Method> methodMap = new HashMap<String, Method>(
1);
public DynamicHandler(Object handler)
{
this.handlerRef = new WeakReference<Object>(handler);
}
public void addMethod(String name, Method method)
{
methodMap.put(name, method);
}
public Object getHandler()
{
return handlerRef.get();
}
public void setHandler(Object handler)
{
this.handlerRef = new WeakReference<Object>(handler);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
Object handler = handlerRef.get();
if (handler != null)
{
String methodName = method.getName();
method = methodMap.get(methodName);
if (method != null)
{
return method.invoke(handler, args);
}
}
return null;
}
}
//通过InvocationHandler设置代理
DynamicHandler handler = new DynamicHandler(activity);
handler.addMethod(methodName, method);
Object listener = Proxy.newProxyInstance(
listenerType.getClassLoader(),
new Class<?>[] { listenerType }, handler);
package com.zhy.invocationhandler;
public class Button
{
private OnClickListener listener;
public void setOnClickLisntener(OnClickListener listener)
{
this.listener = listener;
}
public void click()
{
if (listener != null)
{
listener.onClick();
}
}
}
package com.zhy.invocationhandler;
public interface OnClickListener
{
void onClick();
}
package com.zhy.invocationhandler;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class OnClickListenerHandler implements InvocationHandler
{
private Object targetObject;
public OnClickListenerHandler(Object object)
{
this.targetObject = object;
}
private Map<String, Method> methods = new HashMap<String, Method>();
public void addMethod(String methodName, Method method)
{
methods.put(methodName, method);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
String methodName = method.getName();
Method realMethod = methods.get(methodName);
return realMethod.invoke(targetObject, args);
}
}
package com.zhy.invocationhandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Main
{
private Button button = new Button();
public Main() throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
{
init();
}
public void click()
{
System.out.println("Button clicked!");
}
public void init() throws SecurityException,
NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException
{
OnClickListenerHandler h = new OnClickListenerHandler(this);
Method method = Main.class.getMethod("click", null);
h.addMethod("onClick", method);
Object clickProxy = Proxy.newProxyInstance(
OnClickListener.class.getClassLoader(),
new Class<?>[] { OnClickListener.class }, h);
Method clickMethod = button.getClass().getMethod("setOnClickLisntener",
OnClickListener.class);
clickMethod.invoke(button, clickProxy);
}
public static void main(String[] args) throws SecurityException,
IllegalArgumentException, NoSuchMethodException,
IllegalAccessException, InvocationTargetException
{
Main main = new Main();
main.button.click();
}
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
String methodName = method.getName();
Method realMethod = methods.get(methodName);
return realMethod.invoke(targetObject, args);
}
//通过InvocationHandler设置代理
DynamicHandler handler = new DynamicHandler(activity);
//往map添加方法
handler.addMethod(methodName, method);
Object listener = Proxy.newProxyInstance(
listenerType.getClassLoader(),
new Class<?>[] { listenerType }, handler);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有