![]()
![]()
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 }