美文网首页
【注解】使用注解来代替findViewById

【注解】使用注解来代替findViewById

作者: 秀叶寒冬 | 来源:发表于2020-03-04 16:41 被阅读0次

本文是使用注解代替findViewById的简单使用,在此之前,必须要了解什么是元注解,元注解有哪些,作用是什么?

注解的概念是java5.0提出来的,元注解主要有四种:

  • @Target:说明了注解修饰的范围
  • @Retention:定义了注解被保留的时间
  • @Documented:表示可以被诸如javadoc此类工具文档化
  • @Inherited:阐述了某个被标注的类型是被继承的

具体可参考:【注解】自定义注解及元注解

  • 首先定义一个BindView注解类
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created by yds
 * on 2020/3/4.
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BindView {
    int value();
}

  • 定义一个注解实现类DragonFly
import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.annotation.UiThread;

import java.lang.reflect.Field;

/**
 * Created by yds
 * on 2020/3/4.
 */
public class DragonFly {
    @NonNull
    @UiThread
    public static void bind(@NonNull Activity target) {
        Class<?> clazz = target.getClass();
        Field[] fields = clazz.getDeclaredFields();
        if (fields == null || fields.length == 0) {
            return;
        }
        for (Field field : fields) {
            if (field.isAnnotationPresent(BindView.class)) {
                BindView bindView = field.getAnnotation(BindView.class);
                int value = bindView.value();
                if (value > 0) {
                    field.setAccessible(true);
                    try {
                        field.set(target, target.findViewById(value));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}

  • 使用
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import com.example.annotationdemo.annotationdefine.BindView;
import com.example.annotationdemo.annotationdefine.DragonFly;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.tv)
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DragonFly.bind(this);

        mTextView.setText("测试");
    }
}

相关文章

  • 【注解】使用注解来代替findViewById

    本文是使用注解代替findViewById的简单使用,在此之前,必须要了解什么是元注解,元注解有哪些,作用是什么?...

  • Android-注解

    1.解释: 注解只是一个标识(标记),没有具体的功能代码。 2.通过注解和反射实现findViewById 使用:

  • Android Ioc 框架(ViewJet)

    前言 黄油刀(BufferKnife)使用注解声明控件,不再反复 findViewById,使得代码简洁高效。下面...

  • 动手撸一个ButterKnife

    核心思想 使用注解,提供开始注入的方法,找到注解上的id值,findViewById找该id值。 解决方法 自定义...

  • 10 Mybatis注解开发 多表查询

    复杂关系映射的注解说明 @Results 注解 代替的是标签该注解中可以使用单个@Resu...

  • spring(二)

    用注解代替xml配置 1.首先在配置文件中打开注解配置xml的开关 2.在Bean的类中使用注解 3.在注解中功能...

  • Kotlin注解入门

    Kotlin使用annotation class关键字来定义注解。 一、定义注解 Kotlin不允许为注解定义注解...

  • Java注解

    1. 注解的定义 注解是元数据,是修饰数据的数据,在Java中使用注解来修饰类、方法或者属性,然后使用注解处理器来...

  • 使用注解配置Servlet3.0

    从Servlet3.0开始支持使用注解来配置。 注解只是代替了一部分的web.xml的 配置,通常在针对单个Ser...

  • Android注解框架butterknife基本用法

    现在安卓项目开发中,butterknife是比较常用的注解框架,从而简化了findViewById的重复使用,提高...

网友评论

      本文标题:【注解】使用注解来代替findViewById

      本文链接:https://www.haomeiwen.com/subject/yshblhtx.html