EventBus两个页面传值

//首先就是要导入依赖

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'org.greenrobot:eventbus:3.0.0'

//主页面

@BindView(R.id.name)
EditText name;
@BindView(R.id.age)
EditText age;
@BindView(R.id.but)
Button but;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
}

@OnClick(R.id.but)
public void onViewClicked() {
    String s = name.getText().toString();
    String a = this.age.getText().toString();
    MesageEventBus mesageEventBus = new MesageEventBus();
    mesageEventBus.setName(s);
    mesageEventBus.setAge(a);
    EventBus.getDefault().postSticky(mesageEventBus);
    Intent intent = new Intent(MainActivity.this, Success.class);
    startActivity(intent);
}

//接收的页面

@BindView(R.id.su_name)
EditText suName;
@BindView(R.id.su_age)
EditText suAge;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_success);
    ButterKnife.bind(this);
    EventBus.getDefault().register(this);
}
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void onMessage(MesageEventBus eventBus){
    suName.setText(eventBus.getName());
    suAge.setText(eventBus.getAge());
}
@Override
protected void onDestroy() {
    super.onDestroy();
    EventBus.getDefault().unregister(this);
}

//自己定义的类参数

public class MesageEventBus {

    private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

你可能感兴趣的:(EventBus两个页面传值)