版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bbtianshi/article/details/80281785
第一步假如依赖
implementation 'org.greenrobot:eventbus:3.1.1'
对应的创建一个 sendActivity 和ReceiveActivity 一个发送一个接收
对应的SendActivity的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.samsung.ceshi.Eventbas.SendActivity">
<EditText
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/send"
android:text="发送"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
activity 发送方
public class SendActivity extends AppCompatActivity {
EditText value;
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
value = (EditText) findViewById(R.id.value);
send = (Button)findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = value.getText().toString();
EventBus.getDefault().postSticky(new Bas(s));
Intent intent = new Intent(SendActivity.this,ReceiveActivity.class);
startActivity(intent);
}
});
}
}
对应的接收方 ReceiveActivity 以及对应的布局
public class ReceiveActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
tv = (TextView)findViewById(R.id.tv);
//注册事件
if (!EventBus.getDefault().isRegistered(ReceiveActivity.this)){
EventBus.getDefault().register(this);
}
}
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void Event(Bas ge) {
ge.getId();
tv.setText(ge.getId()+"");
}
@Override
protected void onDestroy() {
super.onDestroy();
if(EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this);
}
}
}
对应的 ReceiveActivity布局xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.samsung.ceshi.Eventbas.ReceiveActivity">
<TextView
android:id="@+id/tv"
android:text="测试"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.design.widget.CoordinatorLayout>
重要的一点 注意 要对应创建一个 一个bean类 ,自己的需求 和字段
public class Bas {
private String id;
public Bas(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
这个是对应的粘性时间
还有intent传值 代码 https://blog.csdn.net/bbtianshi/article/details/80225974
扫描二维码关注公众号,回复:
2989173 查看本文章