码迷,mamicode.com
首页 > 移动开发 > 详细

Android_Event Bus 的基本用法

时间:2017-06-24 19:53:29      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:16px   android   font   用法   nbsp   tty   activity   post   发送   

技术分享

 

技术分享

 

 

 1 //事件总线分发
 2 public class MainActivity extends ActionBarActivity {
 3     Button button;
 4     TextView text;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.fragment_main);
10         
11         button = (Button) findViewById(R.id.button1);
12         text = (TextView) findViewById(R.id.textView1);
13         EventBus.getDefault().register(this);// 注册
14     button.setOnClickListener(new OnClickListener() {
15         // 发送事件
16         @Override
17         public void onClick(View v) {
18             MyEvent my=new MyEvent();
19             my.setType("0");
20             my.setContent("0内容");
21     EventBus.getDefault().post(my);
22         }
23     });
24     }
25 
26     
27 
28     // 接收数据消息事件
29 //    public void onEvent(MyEvent event) {
30 //        if (event.getType().equals("0")) {
31 //            text.setText(event.getContent());
32 //        }
33 //
34 //    }
35 
36     public void onEventMainThread(MyEvent event) {
37         if (event.getType().equals("0")) {
38             text.setText(event.getContent());
39         }
40     }
41 //
42 //    public void onEventPostThread(String string) {
43 //
44 //    }
45 //
46 //    public void onEventBackgroundThread(String string) {
47 //
48 //    }
49 //
50 //    public void onEventAsync(String string) {
51 //
52 //    }
53 
54     @Override
55     protected void onDestroy() {
56         // TODO Auto-generated method stub
57         super.onDestroy();
58         EventBus.getDefault().unregister(this);// 取消注册
59     }
60 
61 }

 

 

 

Android_Event Bus 的基本用法

标签:16px   android   font   用法   nbsp   tty   activity   post   发送   

原文地址:http://www.cnblogs.com/my334420/p/7074130.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!