Android custom annotations (1)

We create a new interface:

public interface BindView {
}

At this time, if we add an @ symbol in front of the interface, at this time, we can see that the file symbol of the IDE has become a comment:

public @interface BindView {
}

Next, we mark where it is used in the annotation:

@Target(ElementType.FIELD)
public @interface BindView {
}

In this way, we can use it on the field, for example, we use it like this in the activity:

public class MainActivity4 extends AppCompatActivity {

    @BindView
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
    }
}

What if I want to use it on multiple types? Add curly braces:

@Target({ElementType.FIELD, ElementType.METHOD})

At this time, we can not only use it on fields, but also on methods:

public class MainActivity4 extends AppCompatActivity {

    @BindView
    TextView textView;

    @BindView
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
    }
}

In fact, what is the main function of annotations? Mainly used for identification. We can pass this logo later. To obtain the annotations through reflection or annotators.

Next, we need to mark the life cycle of annotations:

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
public @interface BindView {
}

Here, there are three life cycles. SOURCE represents that it is only marked in the source code stage and compiled into a CLASS file.

CLASS stage: If it is a CLASS stage, it will be reserved to the compiled CLASS file. But it doesn't exist after the JVM loads our CLASS file.

RUNTIME stage: All periods, annotation information exists.

Add something to the annotation:

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
public @interface BindView {
    int value();
}

For example, we added a method whose return value is Int.

Then when we use it, we need to pass parameters in:

public class MainActivity4 extends AppCompatActivity {

    @BindView(1)
    TextView textView;

    @BindView(2)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
    }
}

If multiple methods are written in the annotation:

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
public @interface BindView {
    String value();
    int id();
}

use:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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

import com.example.myapplication.annotation.BindView;

public class MainActivity4 extends AppCompatActivity {

    @BindView(value = "text",id =1)
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
    }
}

What is the role of annotations for?

/**
 * 注解,单独是没意义的
 * 一般是注解+APT(注解处理器) 用于生成一些JAVA文件 比如butterknife dagger2 hilt
 * 注解+代码埋点 ASpectJ arouter
 * 注解+反射
 */

Guess you like

Origin blog.csdn.net/howlaa/article/details/129678888