EventBus基本使用
来源:互联网 发布:agile java pdf 编辑:程序博客网 时间:2024/06/01 15:17
1.首先添加依赖
compile 'org.greenrobot:eventbus:3.0.0'
2.我们要创建一个类 , 里面写我们要传送的属性
public class FirstEvent { private String msg; public FirstEvent(String msg) { this.msg = msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }}
3.然后我们要在mainactivity中注册eventbus
public class MainActivity extends AppCompatActivity { private Button button; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.BT); textView = (TextView) findViewById(R.id.TV); //注册EventBus EventBus.getDefault().register(this); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this , MyActivity.class); startActivity(intent); } }); } //EventBus3.0必须要加上此注解 否则会报错 @Subscribe public void onEventMainThread(FirstEvent firstEvent){ String msg = "接收到的消息"+firstEvent.getMsg(); textView.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } @Override protected void onDestroy() { super.onDestroy(); //解除注册 EventBus.getDefault().unregister(this); }}
4.我们在另一个activity中用来发送消息
public class MyActivity extends AppCompatActivity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); button = (Button) findViewById(R.id.btn_first_event); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //发送消息 EventBus.getDefault().post(new FirstEvent("chen xue da sha bi")); finish(); } }); }}
5.两个布局 主布局是一个button , textview 另一个布局是一个button
阅读全文
0 0
- Eventbus的基本使用
- Android EventBus 基本使用
- EventBus基本使用简介
- EventBus的基本使用
- eventbus基本使用
- EventBus的基本使用
- EventBus基本使用
- EventBus基本使用
- Android EventBus的基本使用
- Android:EventBus-3 基本使用
- 深入理解EventBus - 基本使用
- EventBus(1)——基本使用
- EventBus深入浅析之基本使用
- EventBus 基本使用和进阶配置
- EventBus的基本使用及原理
- Android的EventBus和butterknife基本使用
- EventBus源码详解(一):基本使用
- EventBus完全解析(一)基本使用
- virtalbox Linux ubntu centos随笔
- android 开发坑点记录
- [leetcode]484.Find Permutation
- 4.jupyter的常用操作(下)
- sql笔记
- EventBus基本使用
- mysql_old_wrong
- 知识付费工具:智果内容付费系统是什么?
- 文本框限制只能输入数字并添加千位分隔符
- MindManager之人大附中学习法则
- 我的第一棵平衡树-SB树
- AlertDialog-的使用
- 2017-11-7离线赛总结(NOIP七连测第五场)
- 03_Java基础语法_第3天(Scanner、Random、流程控制语句)