Android_Event Bus 的基本用法
生活随笔
收集整理的這篇文章主要介紹了
Android_Event Bus 的基本用法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
?
?
1 //事件總線分發(fā) 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);// 注冊(cè) 14 button.setOnClickListener(new OnClickListener() { 15 // 發(fā)送事件 16 @Override 17 public void onClick(View v) { 18 MyEvent my=new MyEvent(); 19 my.setType("0"); 20 my.setContent("0內(nèi)容"); 21 EventBus.getDefault().post(my); 22 } 23 }); 24 } 25 26 27 28 // 接收數(shù)據(jù)消息事件 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);// 取消注冊(cè) 59 } 60 61 }?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/my334420/p/7074130.html
總結(jié)
以上是生活随笔為你收集整理的Android_Event Bus 的基本用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 浅谈传统企业网络运营那些事儿
- 下一篇: Android 手把手带你玩转自己定义相