Android应用中使用运行时注解
来源:互联网 发布:淘宝助理教学 编辑:程序博客网 时间:2024/06/07 03:22
其实非常简单,直接上代码:本文主要是替代传统的findViewById()的功能,就是在我们Activity中不需要再使用findViewById()去给View赋值了,通过注解在运行阶段自动赋值。以及setOnClickListener()也是一样的原理。使用注解和反射技术。
1. 定义自己的annotation注解。
定义findViewbyId这个功能的注解
package com.xxx.app.inject;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 InjectView { int value() default (int) -1;}
定义setOnclickListener的注解
package com.xxx.app.inject;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)public @interface InjectClick { int[] value();}2. 定义自己的注解处理类:
package com.xxx.app.inject;import android.app.Activity;import android.view.View;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.Method;public class Injector { public static void injectView(Object obj, Object root) { Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); Annotation[] annotations = field.getAnnotations(); if (annotations != null) { for (Annotation annotation : annotations) { if (annotation instanceof InjectView) { InjectView injectView = (InjectView) annotation; int value = injectView.value(); if (value != -1) { try { View view = getViewByRoot(root, value); field.set(obj, view); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } break; } } } } } public static void injectClick(Object obj, Object root) { Method[] methods = obj.getClass().getDeclaredMethods(); for (Method method : methods) { Annotation[] annotations = method.getAnnotations(); if (annotations != null) { for (Annotation annotation : annotations) { if (annotation instanceof InjectClick) { InjectClick inject = (InjectClick) annotation; int[] value = inject.value(); if (value != null && value.length > 0) { View.OnClickListener listener = (View.OnClickListener) obj; try { for (int res : value) { View view = getViewByRoot(root, res); if (view == null) { throw new NullPointerException(); } view.setOnClickListener(listener); } } catch (IllegalArgumentException e) { e.printStackTrace(); } } } else if (annotation instanceof InjectLongClick) { InjectLongClick inject = (InjectLongClick) annotation; int[] value = inject.value(); if (value != null && value.length > 0) { View.OnLongClickListener listener = (View.OnLongClickListener) obj; try { for (int res : value) { View view = getViewByRoot(root, res); if (view == null) { throw new NullPointerException(); } view.setOnLongClickListener(listener); } } catch (IllegalArgumentException e) { e.printStackTrace(); } } } } } } } public static View getViewByRoot(Object root, int res) { View view = null; if (root instanceof Activity) { view = ((Activity)root).findViewById(res); } return view; }}
3. Activity中使用注解:
@InjectView(R.id.action_back)private ImageView actionBack;@InjectView(R.id.site_top_bg)private ImageView mSiteTopBg;@InjectView(R.id.site_name)private TextView mSiteName;@InjectView(R.id.site_producter)private TextView mProducter;@InjectView(R.id.site_logo)private ImageRoundView mSiteLogo;@InjectView(R.id.site_description)private TextView mSiteDescription;@InjectView(R.id.viewPager)private ViewPager mViewPager;
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_site); Injector.injectView(this, this); Injector.injectClick(this, this); }
@Override@InjectClick({R.id.action_back})public void onClick(View v) { int id = v.getId(); switch (id){ case R.id.action_back: SiteDetailActivity.this.finish(); break; }}
0 1
- Android应用中使用运行时注解
- Android 框架学习1:什么是注解以及运行时注解的使用
- Android 中注解的使用
- Android中的自定义注解(反射实现-运行时注解)
- Android中的自定义注解(反射实现-运行时注解)
- Android中的自定义注解(反射实现-运行时注解)
- Android中的自定义注解(反射实现-运行时注解)
- Android中的自定义注解(反射实现-运行时注解)
- 什么是注解以及运行时注解的使用
- Android使用monitor监控应用运行时状态(一)
- Android Studio中使用ButterKnife注解框架
- Java注解在Android中使用
- android中 IntDef注解的使用
- Android中使用注解替代枚举
- Android 中Java注解的使用
- android library中使用butterknife的注解
- Android Studio中butterknife注解的使用
- Android studio中使用butterknife注解
- Binder设计与实现
- java 流(二)
- 20160422 css 字体大小设置
- 常量const详解
- tomcat内存溢出
- Android应用中使用运行时注解
- watchOS 2
- 动画1
- c++第4次作业
- Linux Nginx升级
- c++第四次实验-2
- js清除浏览器缓存的几种方法
- cc、gcc、g++、CC的区别概括
- SDAU 课程练习3 1008