在主活动中(订阅方)
package com.example.eventbus;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
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 TextView textView;
private Button button_skip;
private Button button_login;
private ImageView imageView;
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMoonEvent(MesageEvent s2){
// textView.setText(s2.getMessage());
imageView.setImageResource(s2.getImg());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.main_text);
imageView = findViewById(R.id.main_imageview);
button_login = findViewById(R.id.main_button_login);
button_skip = findViewById(R.id.main_button_skip);
/**
* 跳转
*/
button_skip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
});
/**
* 注册
*/
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().register(MainActivity.this);
}
});
/**
* 事件回调
*/
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
MesageEvent这是一个消息类
package com.example.eventbus;
import androidx.annotation.VisibleForTesting;
public class MesageEvent {
private int img;
public MesageEvent(int img) {
this.img = img;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
}
SecondActivity 发送方
package com.example.eventbus;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import org.greenrobot.eventbus.EventBus;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button button = findViewById(R.id.search_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(new MesageEvent(R.drawable.ic_android_black_24dp));
finish();
}
});
}
}
xml——activity_main
<?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=".MainActivity">
<TextView
android:id="@+id/main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_marginLeft="200dp"
/>
<Button
android:id="@+id/main_button_skip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转"/>
<Button
android:id="@+id/main_button_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"/>
<ImageView
android:id="@+id/main_imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
——
</LinearLayout>
activity_second
<?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"
tools:context=".SecondActivity">
<Button
android:id="@+id/search_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送"/>
</LinearLayout>