个人学习,仅供菜鸟们学习!
实例:
首先添加依赖:
//EventBus依赖
compile 'org.greenrobot:eventbus:3.0.0'
然后创建布局:
然后创建activity消息类
创建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("面对疾风吧!"));
}
});
}
}
完毕!!!项目名称