欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android AOP 注解详解及简单使用实例(三)

程序员文章站 2023-12-03 07:58:21
android  注解 相关文章: android aop注解annotation详解(一) android aop之注解处理解释器详解(二) andro...

android  注解

相关文章:

android aop注解annotation详解(一)
android aop之注解处理解释器详解(二)
android aop 注解详解及简单使用实例(三)

一、简介

在android 里面 注解主要用来干这么几件事:

  1. 和编译器一起给你一些提示警告信息。
  2. 配合一些ide 可以更加方便快捷 安全有效的编写java代码。谷歌出的support-annotations这个库 就是主要干这个的。
  3. 和反射一起 提供一些类似于spring 可配置的功能,方便简洁。

二、support annotations栗子

这里使用官方的一个库,说明在开发中的简单一个应用。

2.1 导包

在新建项目的时候会自动导的,可以看build.gradle中的依赖dependencies是这样的。

dependencies {
 compile filetree(dir: 'libs', include: ['*.jar'])
 androidtestcompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  exclude group: 'com.android.support', module: 'support-annotations'
 })
 compile 'com.android.support:appcompat-v7:25.1.1'
 testcompile 'junit:junit:4.12'
}

如果没有的话,自己在build.gradle的依赖添加(xx.x.x为你的compilesdkversion版本号):

compile 'com.android.support:support-annotations:xx.x.x'

2.2 使用

这时候就可以使用一些support-annotations提供的注解,下面举一些栗子:

1、 @nonnull

test方法参数添加了一个nonnull注解,然后我们传递一个空的参数过去。

public class mainactivity extends appcompatactivity {

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  string s = null;
  test(s);
 }

 public void test(@nonnull string s){
  system.out.println(s);
 }

}

ide就会提示警告

Android AOP 注解详解及简单使用实例(三)

2、 @stringres

再定义teststring方法参数添加了一个stringres注解,然后我们传递一个数字过去。

public class mainactivity extends appcompatactivity {

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  teststring(112312);
  teststring(r.string.app_name);
 }

 public void teststring(@stringres int s){
  system.out.println(s);
 }

}

ide就会提示

Android AOP 注解详解及简单使用实例(三)

三、实现自己的butterknife

经过之前的知识,我们已经知道注解的原理和使用了,这里实现butterknife的一个简单功能,view的注入: 一个注解,一个解析器即可。

3.1 bindview注解

@target(elementtype.field) //解析常量
@retention(retentionpolicy.runtime) //运行时
public @interface bindview {
  int value() default -1; //标识控件
}

3.2 bindviewparser解析器

/**
 * created by litp on 2017/2/17.
 */
public class bindviewparser {

 /**
  * 传递activty或者view 对象,使用反射获取view变量
  * @param object
  */
 public static void inject(object object) {

  try {
   parse(object);
  } catch (exception e) {
   e.printstacktrace();
  }
 }

 /**
  * 解析获取值
  * @param object
  * @throws exception
  */
 public static void parse(object object) throws exception {

  final class<?> clazz = object.getclass();

  view view = null;

  //获取clazz的变量,不论private还是public
  field[] fields = clazz.getdeclaredfields();

  for (field field : fields) {

   //这个变量 是否有bindview注解
   if (field.isannotationpresent(bindview.class)) {
    //获取这个变量对应的注解
    bindview injectview = field.getannotation(bindview.class);
    //获取值
    int id = injectview.value();
    if (id <= 0) {
     throw new exception("view的id不能为空");
    } else {
     //设置可以访问
     field.setaccessible(true);
     //获取view
     if (object instanceof view) {
      view = ((view) object).findviewbyid(id);
     } else if (object instanceof activity) {
      view = ((activity) object).findviewbyid(id);
     }
     //设置view
     field.set(object, view);
    }

   }

  }

 }


}

3.3 activity使用

public class mainactivity extends appcompatactivity {

 //使用注解标识变量
 @bindview(r.id.tv_test)
 textview textview;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  //传递当前activty给解析器,进行初始化view
  bindviewparser.inject(this);

  //这里就已经是初始化完毕了,可以进行使用了
  textview.settext("测试自己的注入demo");

 }


}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!