个人学习,仅供菜鸟们学习!

 

 

第三方库EventBus消息传递的使用

第三方库EventBus消息传递的使用

第三方库EventBus消息传递的使用

实例:

第三方库EventBus消息传递的使用

 

 

第三方库EventBus消息传递的使用第三方库EventBus消息传递的使用

 

 

首先添加依赖:

//EventBus依赖
compile 'org.greenrobot:eventbus:3.0.0'
然后创建布局:

第三方库EventBus消息传递的使用

第三方库EventBus消息传递的使用

然后创建activity消息类

第三方库EventBus消息传递的使用

创建MainActivity

package com.fuicuiedu.xc.eventbus_20170307;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {

TextView mTv;

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

//EventBus在监听事件模块完成注册
EventBus.getDefault().register(this);

mTv = (TextView) findViewById(R.id.main_tv);

findViewById(R.id.mian_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void aaa(MessageEvent messageEvent){
String msg = messageEvent.getMsg();
//弹吐司
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
//更新UI
mTv.setText(msg);
}

@Override
protected void onDestroy() {
super.onDestroy();
//取消订阅,反注册
EventBus.getDefault().unregister(this);
}
}

跳转页面:

package com.fuicuiedu.xc.eventbus_20170307;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import org.greenrobot.eventbus.EventBus;

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

findViewById(R.id.second_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送事件
EventBus.getDefault().post(new MessageEvent("面对疾风吧!"));
}
});
}
}

完毕!!!项目名称第三方库EventBus消息传递的使用

 

 

 


 

相关文章:

  • 2021-07-03
  • 2022-12-23
  • 2021-09-02
  • 2021-08-26
  • 2021-11-05
  • 2022-12-23
  • 2022-01-03
  • 2021-09-08
猜你喜欢
  • 2021-05-29
  • 2021-07-26
  • 2021-10-09
  • 2022-12-23
  • 2022-01-06
  • 2022-12-23
相关资源
相似解决方案