Android EventBus发送和接收(运用注解解析获取属性)

首先导入依赖

implementation 'org.greenrobot:eventbus:3.0.0'

MainActivity

package com.example.包名;

import android.content.Intent;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {
    @FindByid(R.id.view)
    private TextView textView;
    @FindByid(R.id.button)
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //注解的一个连接,这样就可以找到我们所需要的属性,如果不写它会报出空指针 29行
        Annaction.setview(this);
        //注册一个eventbus
        EventBus.getDefault().register(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进行跳转
                Intent intent=new Intent(MainActivity.this,SecendActivity.class);
                startActivity(intent);
            }
        });
    }
    //处理事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public  void Event(MessageEvent messageEvent){
        //接收发送回来的数据并显示
        textView.setText(messageEvent.getMessage());
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除eventbus
        EventBus.getDefault().unregister(this);
    }
}

注解

package com.example.包名;
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 FindByid {
  //int 值
  int value();
}

注解解析

package com.example.包名;

import android.view.View;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class Annaction {
    public static void setview(final MainActivity mainActivity) {
        Class aClass=mainActivity.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            boolean annotationPresent = declaredField.isAnnotationPresent(FindByid.class);
            if(annotationPresent){
                FindByid annotation = declaredField.getAnnotation(FindByid.class);
                int value = annotation.value();
                View viewById = mainActivity.findViewById(value);
                declaredField.setAccessible(true);
                try{
                    declaredField.set(mainActivity,viewById);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    public static void setview(SecendActivity mainActivity) {
        Class aClass=mainActivity.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            boolean annotationPresent = declaredField.isAnnotationPresent(FindByid.class);
            if(annotationPresent){
                FindByid annotation = declaredField.getAnnotation(FindByid.class);
                int value = annotation.value();
                View viewById = mainActivity.findViewById(value);
                declaredField.setAccessible(true);
                try{
                    declaredField.set(mainActivity,viewById);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}

EventBus(相当于一个bean类)

package com.example.包名;

public class MessageEvent {
    private String message;

    public MessageEvent(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

发送页面

package com.example.包名;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;

public class SecendActivity extends AppCompatActivity {
    @FindByid(R.id.button2)
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secend);
        Annaction.setview(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEvent("我是被传回来的!"));
                SecendActivity.this.finish();
            }
        });
    }
}

这是没有经过EvenBus发送的效果
Android EventBus发送和接收(运用注解解析获取属性)_第1张图片

第二页面只有一个button按钮所以图省略了 以下是经过EvenBus发送的数据被替代Android EventBus发送和接收(运用注解解析获取属性)_第2张图片

你可能感兴趣的:(Android EventBus发送和接收(运用注解解析获取属性))