[关闭]
@Tyhj 2019-03-13T03:58:38.000000Z 字数 4638 阅读 789

Android中注解的使用

java


注解是开发中经常使用到的,因为很久前在网上找了几篇文章,发现完全看不懂,所以觉得这个东西好像很难搞,最近耐心的看完了这篇文章秒懂,Java 注解你可以这样学,感觉入门还是比较简单的

我为什么想要看注解呢,其实是因为看见有些注解十分方便,比如@ViewById注解可以让代码更优雅,想自己也搞几个注解用一下,比如自动运行在UI线程、子线程的注解;然后发现很多文章讲的太抽象了,而且到最后都根本没有我想要的这种例子,索性就不看了;其实大概就是因为太心急了,没有理解注解的作用;

上面的文章里面的例子很好,把注解比喻成一个标签,注解只是给你这个方法、这个类、这个变量加一个标签而已,然后你可以通过一些方法来获取这个标签,根据自己的需要做一些操作

新建标签

创建一个注解十分简单,和接口差不多

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface AnnotationClass {
  4. int id() default 1;
  5. String name();
  6. }

标签分一些类型,不同的类型给Java中不同的地方标记,类型使用@Target来设置,@Target 有下面的取值

  • ElementType.ANNOTATION_TYPE 可以给一个注解进行注解
  • ElementType.CONSTRUCTOR 可以给构造方法进行注解
  • ElementType.FIELD 可以给属性进行注解
  • ElementType.LOCAL_VARIABLE 可以给局部变量进行注解
  • ElementType.METHOD 可以给方法进行注解
  • ElementType.PACKAGE 可以给一个包进行注解
  • ElementType.PARAMETER 可以给一个方法内的参数进行注解
  • ElementType.TYPE 可以给一个类型进行注解,比如类、接口、枚举

需要使用@Retention来控制标签的生命周期,其实就是你在什么时候去处理这个标签,处理完肯定不要了,不然占空间

  • RetentionPolicy.SOURCE 注解只在源码阶段保留,在编译器进行编译时它将被丢弃忽视。
  • RetentionPolicy.CLASS 注解只被保留到编译进行的时候,它并不会被加载到 JVM 中。
  • RetentionPolicy.RUNTIME 注解可以保留到程序运行的时候,它会被加载进入到 JVM 中,所以在程序运行时可以获取到它们。

然后里面可以设置一些属性,注解的属性也叫做成员变量。注解只有成员变量,没有方法。注解的成员变量在注解的定义中以“无形参的方法”形式来声明,其方法名定义了该成员变量的名字,其返回值定义了该成员变量的类型

运行时标签的使用

上面的标签定义为给类型注解,所以直接在类上面使用就好了,设置了可以在运行时使用,所以可以在程序运行的代码里面获取到标签,但是接下来,获取到标签后要干什么就取决于个人了

  1. @AnnotationClass(name = "Tyhj")
  2. public class Main {
  3. public static void main(String[] args) {
  4. //这个类是否使用了AnnotationClass这个注解
  5. boolean hasAnnotation = Main.class.isAnnotationPresent(AnnotationClass.class);
  6. if (hasAnnotation) {
  7. //获取到AnnotationClass对象
  8. AnnotationClass annotationClass = Main.class.getAnnotation(AnnotationClass.class);
  9. String name = annotationClass.name();
  10. int id = annotationClass.id();
  11. System.out.println("name:" + name + ",id:" + id);
  12. }
  13. }
  14. }

然后对于方法和成员变量的使用方法
AnnotationField

  1. @Target(ElementType.FIELD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface AnnotationField {
  4. String defultValue() default "哈哈哈";
  5. }

AnnotationMethod

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target({ElementType.METHOD})
  3. public @interface AnnotationMethod {
  4. }

简单的获取出标签

  1. @AnnotationClass(name = "Tyhj")
  2. public class Main {
  3. @AnnotationField(defultValue = "嗨,你好")
  4. private String msg;
  5. @AnnotationMethod
  6. private Long getTime() {
  7. return System.currentTimeMillis();
  8. }
  9. public static void main(String[] args) {
  10. Main main = new Main();
  11. //判断这个类是否存在这个注解
  12. boolean hasAnnotation = Main.class.isAnnotationPresent(AnnotationClass.class);
  13. if (hasAnnotation) {
  14. //获取到注解实例
  15. AnnotationClass annotationClass = Main.class.getAnnotation(AnnotationClass.class);
  16. String name = annotationClass.name();
  17. int id = annotationClass.id();
  18. System.out.println("name:" + name + ",id:" + id);
  19. }
  20. //获取成员变量的注解
  21. try {
  22. Field field = Main.class.getDeclaredField("msg");
  23. //msg成员变量为private,故必须进行此操作
  24. field.setAccessible(true);
  25. AnnotationField check = field.getAnnotation(AnnotationField.class);
  26. //这里把注解的内容赋值给变量
  27. main.msg = check.defultValue();
  28. } catch (NoSuchFieldException e) {
  29. e.printStackTrace();
  30. }
  31. //获取方法的注解
  32. try {
  33. Method method = Main.class.getDeclaredMethod("getTime");
  34. method.setAccessible(true);
  35. AnnotationMethod annotationMethod = method.getAnnotation(AnnotationMethod.class);
  36. } catch (NoSuchMethodException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }

都只是简单的展示获取标签,其中获取了获取成员变量的标签的值赋值给了运行的对象,通过这个就对@ViewById(R.id.textView)的实现有点想法了吧

实现运行时标签@ViewById

新建一个标签,当标签只有一个属性并且为value的时候,使用的时候可以不用写名字,用value来保存id,在运行的时候获取出来初始化控件,应该是ok的

  1. @Target(ElementType.FIELD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface ViewById {
  4. int value() default -1;
  5. }

具体的实现,需要找出所有的标签,一个个看一下是不是ViewById标签,是的话对之进行操作

  1. @ViewById(R.id.txtView)
  2. TextView txtView;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. initAnnotation();
  8. txtView.setOnClickListener(v -> Toast.makeText(MainActivity.this, "醉了", Toast.LENGTH_SHORT).show());
  9. }
  10. /**
  11. * 开始获取注解进行操作
  12. */
  13. private void initAnnotation() {
  14. //获得成员变量
  15. Field[] fields = this.getClass().getDeclaredFields();
  16. for (Field field : fields) {
  17. //允许修改反射属性
  18. field.setAccessible(true);
  19. //获取到标签
  20. ViewById viewById = field.getAnnotation(ViewById.class);
  21. if (viewById != null) {
  22. try {
  23. //向对象的这个Field属性设置新值value
  24. field.set(this, findViewById(viewById.value()));
  25. } catch (IllegalAccessException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30. }

甚至我们可以不写ID值使用,但是需要这个变量的名字和ID名字一样,这样我们可以通过这个变量的名字去生成ID,再进行绑定

  1. @ViewById
  2. TextView txtView;
  3. //向对象的这个Field属性设置新值value
  4. if (viewById.value() == -1) {
  5. field.set(this,findViewById(getId(MainActivity.this, field.getName())));
  6. } else {
  7. field.set(this, findViewById(viewById.value()));
  8. }
  9. /**
  10. * 通过名字获取资源文件的id
  11. *
  12. * @param context
  13. * @param resName
  14. * @return
  15. */
  16. public static int getId(Context context, String resName) {
  17. return context.getResources().getIdentifier(resName, "id", context.getPackageName());
  18. }

这样效果就和Butter Knife效果差不多一样了呀,而且可以不写ID,但是这个是运行时注解,一切都是运行时自己来写的,封装一下的确也可以实现这个功能,但是运行时注解效率稍微会低一点,而且看一下Butter Knife的源码,它是CLASS,是编译时注解,在编译的时候生成的代码

  1. @Retention(CLASS) @Target(FIELD)
  2. public @interface BindView {
  3. /** View ID to which the field will be bound. */
  4. @IdRes int value();
  5. }

项目源码:https://github.com/tyhjh/Annotation.git

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注