eventbus传值

//依赖 implementation 'org.greenrobot:eventbus:3.0.0'




//写eventbus 自定义的一个类里面写传的参数 package com.example.eventbus; /** * date:2018/11/29 * author:霍钲杰(叮咯呤咚呛) * function: */ public class UserEvent { private String name; private String password; public UserEvent() { }public UserEvent(String name, String password) { this.name = name; this.password = password; }public String getName() { return name; }public void setName(String name) { this.name = name; }public String getPassword() { return password; }public void setPassword(String password) { this.password = password; }@Override public String toString() { return "UserEvent{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }

//main package com.example.eventbus; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; 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 implements View.OnClickListener {private Button fanhui; private Button viscidity; private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //注册订阅者 if(!EventBus.getDefault().isRegistered(this)){ EventBus.getDefault().register(this); }}private void initView() { fanhui = (Button) findViewById(R.id.fanhui); viscidity = (Button) findViewById(R.id.viscidity); text = (TextView) findViewById(R.id.text); fanhui.setOnClickListener(this); viscidity.setOnClickListener(this); }@Override public void onClick(View v) { switch (v.getId()) { case R.id.fanhui: startActivity(new Intent(MainActivity.this,SendActivity.class)); break; case R.id.viscidity:EventBus.getDefault().postSticky(new UserEvent("粘性事件", "urgent")); startActivity(new Intent(MainActivity.this,NianActivity.class)); break; } }@Subscribe(threadMode = ThreadMode.MAIN) public void reception(UserEvent userEvent){ text.setText(userEvent.toString()); }@Override protected void onDestroy() { super.onDestroy(); //注销注册 EventBus.getDefault().unregister(this); } }

//nian.java package com.example.eventbus; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; 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 NianActivity extends AppCompatActivity implements View.OnClickListener {private Button nian; private TextView text1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_nian); initView(); }private void initView() { nian = (Button) findViewById(R.id.nian); text1 = (TextView) findViewById(R.id.text1); nian.setOnClickListener(this); }@Override public void onClick(View v) { switch (v.getId()) { case R.id.nian: if(!(EventBus.getDefault().isRegistered(NianActivity.this))){ EventBus.getDefault().register(this); } break; } }@Subscribe(threadMode = ThreadMode.MAIN,sticky = true) public void reception1(UserEvent userEvent){ text1.setText(userEvent.toString()); }@Override protected void onStop() { super.onStop(); EventBus.getDefault().unregister(this); } }

//发送事件 package com.example.eventbus; 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 SendActivity extends AppCompatActivity implements View.OnClickListener {private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send); initView(); }private void initView() { btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(this); }@Override public void onClick(View v) { switch (v.getId()) { case R.id.btn: //发送事件 EventBus.getDefault().post(new UserEvent("1111","zrj")); finish(); break; } } }

    推荐阅读