EventBus回传值简单使用

//先导依赖

compile 'org.greenrobot:eventbus:3.0.0'
//一个消息的实体类也就是Bean类
public class EventBusStickMessage {
    public String Message;

    public EventBusStickMessage(String message) {
        Message = message;
    }

}

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 跳转到发送页面的Activity
     */
    private Button mButton;
    private TextView mTvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        EventBus.getDefault().register(this);
    }

    //注解
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void shou(EBMessage ebMessage){
        String str="传递消息"+ebMessage.Message;

        mTvTitle.setText(str);
        Log.d("EventBusThread","MAIN"+Thread.currentThread().getName());
    }
    //注解
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void shou01(EBMessage ebMessage){


        Log.d("EventBusThread","POSTING"+Thread.currentThread().getName());
    }
    //注解
    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void shou02(EBMessage ebMessage){


        Log.d("EventBusThread","ASYNC"+Thread.currentThread().getName());
    }
    //注解
    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void shou03(EBMessage ebMessage){


        Log.d("EventBusThread","BACKGROUND"+Thread.currentThread().getName());
    }

    private void initView() {
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(this);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.button:
                Intent intent=new Intent(MainActivity.this,SendActivity.class);
                startActivity(intent);
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:text="跳转到发送页面的Activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"/>

</RelativeLayout>


package com.example.eventbus__test02;

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

import org.greenrobot.eventbus.EventBus;

public class SendActivity extends AppCompatActivity {

    private RelativeLayout mActivityEventBusSend;
     String Message="asdsfgghjg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);


    }


    public  void  send(View view){
        EventBus.getDefault().post(new  EBMessage(Message));
        finish();
    }
}



<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_event_bus_send"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:onClick="send"
        android:text="向主页面使用EventBus发送一个事件"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/pizifusheng/article/details/80614696