EventBus使用
来源:互联网 发布:淘宝花溪折扣店货真吗 编辑:程序博客网 时间:2024/06/05 18:32
//导入依赖
compile 'org.greenrobot:eventbus:3.1.1'
在接受的方法里注册和反注册 //注册EventBus EventBus.getDefault().register(this); onDestory方法里反注册 EventBus.getDefault().unregister(this);//反注册EventBus 在第二个Activity中进行post发送 //发送的msg是Object类型 EventBus.getDefault().post(Object); //eventbus接收消息 //得有注解!!! 参数的发送的类型 @Subscribe public void onEventMainThread(Object event) { tv.setText(event.toString); }
//详细代码
//MainActivity
public class MainActivity extends AppCompatActivity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); Button btn= (Button) findViewById(R.id.btn); tv = (TextView) findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, Main2Activity.class); startActivity(intent); } }); } @Subscribe public void onEventMainThread(String event) { tv.setText(event); //if (event != null) { // String o= (String) event; // if (o!= 0) { // tv.setText(o); // } //} } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }
//Main2Activity
public class Main2Activity extends AppCompatActivity {[java] view plain copy @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Button btn= (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { EventBus.getDefault().post("你好"); } }); } }
//Mian2Activity布局
<RelativeLayout 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"> <Button android:id="@+id/btn" android:layout_below="@id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
//MainActivity布局
<RelativeLayout 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"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:id="@+id/btn" android:layout_below="@id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
阅读全文
0 0
- EventBus 使用
- EventBus使用
- eventbus使用
- EventBus使用
- EventBus---使用
- EventBus使用
- EventBus 使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- Codeforces 165C(二分)
- JavaScript 数组的操作方法
- 第 4 章 选择合适的Python开发环境
- Hadoop完全分布式集群环境搭建及测试
- HTML页面在IOS上出现卡顿的解决方案(overflow:scroll)
- EventBus使用
- Java虚拟机对象创建及其内存分配
- 第一阶段-入门详细图文讲解tensorflow1.4 -(十)TensorBoard: Graph Visualization
- 详解 ML2 Core Plugin(7)
- 【BIM知识】BIM建模师都干什么?(BIM+数据库)
- 使用专用示波器和软件加快测试开关模式电源---凯利讯半导体
- Java中finalize()详解和Java9中的垃圾回收
- 理解正常代理与反向代理的区别
- 二级购物车