EventBus

在主活动中(订阅方)

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值