android 利用反射和注解绑定控件id和点击事件

以前写过一个工具类,利用java的反射和注解绑定安卓控件id和点击事件。 支持绑定activity、fragment、view的控件。记录下,后续抄着用。代码如下。

注解ViewId,用于绑定控件id:

java 复制代码
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 ViewId {
	int id();
}

注解OnClick, 用于绑定控件点击事件:

java 复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 方法传一个view参数,或者不传参数
 * */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Onclick {
	int[] id();
}

工具类ViewUtil,绑定控件id、点击事件:

java 复制代码
import java.lang.reflect.Field;
import java.lang.reflect.Method;


import android.app.Activity;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;

public class ViewUtil {
	Object widget;

	public void init(Activity activity) {
		widget = activity;
		init();
	}

	public void init(View view) {
		widget = view;
		init();
	}

	public void init(Fragment fragment) {
		widget = fragment;
		init();
	}

	private void init() {
		Field[] fields = widget.getClass().getDeclaredFields();
		if (fields != null && fields.length > 0) {
			for (Field field : fields) {
				ViewId viewId = field.getAnnotation(ViewId.class);
				if (viewId != null) {
					try {
						View view = findViewById(viewId.id());
						if (view != null) {
							field.setAccessible(true);
							field.set(widget, view);
						}
					} catch (Exception e) {
						Loger.e(e.getMessage());
						Loger.e("反射字段 "+field.getName()+" 获取异常");
						throw new RuntimeException(e);
					}
				}

			}
		}

		Method[] methods = widget.getClass().getDeclaredMethods();
		if (methods != null && methods.length > 0) {
			for (final Method method : methods) {
				Onclick onclick = method.getAnnotation(Onclick.class);
				if (onclick != null) {
					try {
						int[] ids = onclick.id();
						if (ids != null && ids.length > 0) {
							for (int i = 0; i < ids.length; i++) {
								final View view = findViewById(ids[i]);
								if (view != null) {
									method.setAccessible(true);
									view.setOnClickListener(new View.OnClickListener() {

										@Override
										public void onClick(View v) {
											try {
												Class<?>[] parameterTypes = method
														.getParameterTypes();
												Log.e("zx","parameterTypes length"
														+ parameterTypes.length);
												if (parameterTypes == null
														|| parameterTypes.length == 0) {
													method.invoke(widget);
												} else if (parameterTypes != null
														&& parameterTypes.length == 1
														&& parameterTypes[0]==
																View.class) {
													method.invoke(widget, view);
												} else {
													throw new RuntimeException(
															"传参异常");
												}
											} catch (Exception e) {
												Loger.e(e.getMessage());
												throw new RuntimeException(e);
											}
										}
									});
								}
							}
						}
					} catch (Exception e) {
						Loger.e(e.getMessage());
						throw new RuntimeException(e);
					}
				}
			}
		}
	}

	private View findViewById(int id) {
		if (widget != null && widget instanceof Activity) {
			return ((Activity) widget).findViewById(id);
		}
		if (widget != null && widget instanceof View) {
			return ((View) widget).findViewById(id);
		}
		if (widget != null && widget instanceof Fragment) {
			return ((Fragment) widget).getView().findViewById(id);
		}
		Loger.e("组件不支持");
		throw new RuntimeException("组件不支持");
	}

}

使用方法:略。

相关推荐
游戏开发爱好者83 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
王码码20353 小时前
Flutter for OpenHarmony 实战之基础组件:第三十一篇 Chip 系列组件 — 灵活的标签化交互
android·flutter·交互·harmonyos
黑码哥3 小时前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder
亓才孓3 小时前
[JDBC]元数据
android
独行soc4 小时前
2026年渗透测试面试题总结-17(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
金融RPA机器人丨实在智能4 小时前
Android Studio开发App项目进入AI深水区:实在智能Agent引领无代码交互革命
android·人工智能·ai·android studio
科技块儿4 小时前
利用IP查询在智慧城市交通信号系统中的应用探索
android·tcp/ip·智慧城市
独行soc4 小时前
2026年渗透测试面试题总结-18(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
王码码20355 小时前
Flutter for OpenHarmony 实战之基础组件:第二十七篇 BottomSheet — 动态底部弹窗与底部栏菜单
android·flutter·harmonyos
2501_915106325 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview