刚开始使用的时候出了一个下面这个错误,好特么曹丹,结果是因为按钮冲突,必须用
btn.setOnClickListener(this);
然后继承使用
@Override public void onClick(View v) { //只能注册一次,判断 if (!EventBus.getDefault().isRegistered(this)){ //注册 EventBus.getDefault().register(this); } }不然一直报错。。。
下面是代码
数据类
package com.example.lx_eventbus_chuanzhi; /** * data:2018/06/07. * author : 殷成龙(Administrator) * function : */ public class EventBus1 { public String bus; public EventBus1(String bus) { this.bus = bus; } }
主页面
package com.example.lx_eventbus_chuanzhi; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import org.greenrobot.eventbus.EventBus; import java.util.Random; public class MainActivity extends AppCompatActivity { private Button btn; private String uu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //随机数 Random random = new Random(); //强转 uu = String.valueOf(random.nextInt()); btn = findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //粘性 //传值 EventBus.getDefault().postSticky(new EventBus1(uu)); startActivity(new Intent(MainActivity.this,Main2Activity.class)); } }); } }
布局
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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_chuanzhi.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击跳转传值" android:gravity="center" android:id="@+id/btn"/> </android.support.constraint.ConstraintLayout>
第二个界面
package com.example.lx_eventbus_chuanzhi; 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 Main2Activity extends AppCompatActivity implements View.OnClickListener { private Button btn; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); btn = findViewById(R.id.btn); tv = findViewById(R.id.tv); // EventBus.getDefault().register(this); btn.setOnClickListener(this); /* btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //只能注册一次,判断 if (!EventBus.getDefault().isRegistered(this)){ //注册 EventBus.getDefault().register(this); } } });*/ } //注解 @Subscribe(threadMode = ThreadMode.MAIN,sticky = true) public void Data(EventBus1 eventBus1){ //赋值 tv.setText(eventBus1.bus); } @Override protected void onDestroy() { super.onDestroy(); //解除 EventBus.getDefault().unregister(this); //取消所有粘性依赖的事件 EventBus.getDefault().removeAllStickyEvents(); } @Override public void onClick(View v) { //只能注册一次,判断 if (!EventBus.getDefault().isRegistered(this)){ //注册 EventBus.getDefault().register(this); } } }
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" 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_chuanzhi.Main2Activity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确定获取" android:layout_gravity="center" android:gravity="center" android:id="@+id/btn"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_gravity="center" android:id="@+id/tv" android:gravity="center"/> </LinearLayout>
依赖
/*组件与组件,线程与线程间的通讯*/ compile 'org.greenrobot:eventbus:3.0.0'