主页面
package com.example.lx_eventbus; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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 { private Button btn; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取id btn = findViewById(R.id.button); tv = findViewById(R.id.tv_title); //注册EventBus EventBus eventBus = EventBus.getDefault(); eventBus.register(this); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //跳转到第一个页面 startActivity(new Intent(MainActivity.this,Main2Activity.class)); } }); } //注解:是给代码看的 @Subscribe(threadMode = ThreadMode.MAIN) //接受数据 public void Data(EventBus1 eventBus1){ //赋值 tv.setText(eventBus1.bus); } //停止 @Override protected void onDestroy() { super.onDestroy(); //解除注册 EventBus.getDefault().unregister(this);//反注册EventBus } }
布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lx_eventbus.MainActivity"> <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.lx_eventbus; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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; import java.util.Random; public class Main2Activity extends AppCompatActivity { private Button send; private String uu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //获取id send = findViewById(R.id.send); Random random = new Random(); uu = String.valueOf(random.nextInt()); //点击事件 send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().post(new EventBus1(uu)); finish(); } }); } }
布局
<?xml version="1.0" encoding="utf-8"?> <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:id="@+id/send" android:text="向主页面使用EventBus发送一个事件" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
工具类EventBus1
package com.example.lx_eventbus; /** * data:2018/06/07. * author : 殷成龙(Administrator) * function : */ public class EventBus1 { public String bus; public EventBus1(String bus) { this.bus = bus; } }
依赖
compile 'org.greenrobot:eventbus:3.0.0'