当前位置: 首页 > news >正文

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

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

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


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, 用于绑定控件点击事件:


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、点击事件:


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() {@Overridepublic 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("组件不支持");}}

使用方法:略。

http://www.dtcms.com/a/504531.html

相关文章:

  • windows 网站模板网站流量统计查询
  • 601138(工业富联)-2025年10月19日
  • 桂林企业网站建设做网站的作用
  • 免费网站模块一个人在线观看免费中文
  • 比亚迪召回超11万辆车:质量管控亮起警示灯
  • 企业网站建设推广费用网站里添加百度地图
  • 手机网站模版 优帮云seo整站优化公司持续监控
  • 自己做电视视频网站网站域名和网址
  • sm2025 模拟赛19 (2025.10.14)
  • C 程序的现代方法
  • 学校网站设计论文商标注册网站缴费入口
  • 成都网站建设蜀美网络wordpress vip 插件
  • 怎么用jsp做网站泰州网站设计培训
  • 闵行做网站费用民治网站优化培训
  • 基于trae+花生壳实现网站的公网访问
  • 门户网站建设自查整改报告搜索引擎优化的专家是什么意思
  • 【GESP】C++四级真题 luogu-B4005 [GESP202406 四级] 黑白方块
  • 市场推广方案模板价格优化网站建设
  • 东莞市官网网站建设抚顺网站seo
  • Python数据挖掘之集成技术
  • 网站建设广东猎头公司收费
  • 【Android】BottomNavigationView实现底部导航栏
  • 小程序开发需要多少钱宁波seo外包服务商
  • XSS攻击防范:从入门到精通
  • 做云购网站网站框架怎么做
  • 建中英文网站网络优化公司有哪些
  • 详细讲解java中的方法(适合新手小白)
  • vip解析网站怎么做网络搜索词排名
  • 最新网站域名如何寻找网站建设需求客户
  • 河北网站建设推广如何制作活动宣传网页