<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## (一).前言: 今天我們的項目繼續更新,今天我們主要講解消息總線EventBus的基本使用方法,后面一篇我們會從源碼的角度稍微分析一下實現過程。 FastDev4Android框架項目地址:[https://github.com/jiangqqlmj/FastDev4Android](https://github.com/jiangqqlmj/FastDev4Android) ## (二).簡介: 以前我們做組件間的消息分發更新,一般會采用觀察者模式,或者接口數據回調的相關方式。但是這樣的做法雖然可以解決我們的問題,但是組件之間的耦合比較嚴重,而且代碼也不易閱讀和相關維護。為了解決這樣的問題我們可以使用消息總線EventBus框架。 EventBus是一款針對Android優化的發布/訂閱事件總線。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,線程之間傳遞消息.優點是開銷小,代碼更優雅。以及將發送者和接收者解耦。EventBus開源站點地址:[https://github.com/greenrobot/EventBus](https://github.com/greenrobot/EventBus)。 整個訂閱和接受的架構如下圖: ![](https://box.kancloud.cn/2016-01-18_569c8eb4112fa.jpg) EventBus的特點如下: * 簡化組件間的消息通信 * 使得代碼更加簡潔 * 速度很快 * jar包非常小,不到50K * 還有一些有點例如線程之間的通信,優先級等 ## (三).使用方式 3.1.AndroidStudio進行Gradle配置如下: ~~~ compile 'de.greenrobot:eventbus:2.4.0' ~~~ 3.2.事件對象定義 ~~~ publicclass MessageEvent { /* Additional fields if needed */ } ~~~ 3.3.在接收頁面進行注冊? ?? ~~~ eventBus.register(this); ~~~ 3.4.接收消息方法實現? ? ? ?? ~~~ public voidonEvent(AnyEventType event) {/* Do something */}; ~~~ 3.5.消息發送 ~~~ eventBus.post(event); ~~~ ?????? OK上面是官方的使用說明,現在我們來具體使用一個實例來展示一下EventBus的基本使用。 ## (四).具體事例 4.1.實現需求:在第一個Activity中有一個按鈕和一個TextView,然后點擊按鈕打開第二個Activity,在第二個Activity中有一個按鈕,點擊按鈕關閉當前第二個Activity,同時消息回調到第一個Activity中,在TextView中進行顯示。?? ![](https://box.kancloud.cn/2016-01-18_569c8eb42aaf9.jpg) 4.2.我們這邊需要兩個Activity布局 ~~~ <?xmlversion="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="點擊打開第二個Activity" android:id="@+id/button_one"/> <TextView android:layout_width="wrap_content" android:layout_height="45dp" android:text="這邊顯示消息內容..." android:id="@+id/textView_one" /> </LinearLayout> ~~~ ~~~ <?xmlversion="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二個Activity的按鈕" android:id="@+id/button_two"/> <TextView android:layout_width="wrap_content" android:layout_height="45dp" android:text="當前為第二個Activity" android:id="@+id/textView_two" /> </LinearLayout> ~~~ 4.3.創建一個事件管理類:TestEventFirst.java ~~~ packagecom.chinaztt.fda.event; /** * 當前類注釋:EventBus測試 First事件類 * 項目名:FastDev4Android * 包名:com.chinaztt.fda.event * 作者:江清清 on 15/11/3 14:25 * 郵箱:jiangqqlmj@163.com * QQ: 781931404 * 公司:江蘇中天科技軟件技術有限公司 */ public classTestEventFirst { private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public TestEventFirst(String msg){ this.msg=msg; } } ~~~ 4.4:注冊和取消注冊 使用EventBus.getDefault().register(this);進行注冊 使用EventBus.getDefault().unregister(this);進行取消注冊 4.5.消息發送 使用?EventBus.getDefault().post(new TestEventFirst("我是第二個Activity回傳的信息...."));進行消息發送 4.6.消息接收 在注冊的Activity中進行重寫onEventMainThread()方法來進行處理接收消息(除了這個方法以外,還有另外三個方法,具體我們會在下一篇文章中進行介紹) ~~~ /** * 收到消息 進行相關處理 * @param event */ public voidonEventMainThread(TestEventFirst event) { textView_one.setText(event.getMsg()); showToastMsgShort(event.getMsg()); } ~~~ ???其中方法中的參數TestEventFirst就是發送過來的消息類,具體發送的消息全部已經封裝在里面了。我們只需要使用event對象進行獲取處理即可。 4.7.完整第一個Activity和第二個Activity代碼如下: ~~~ packagecom.chinaztt.fda.test; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.Button; importandroid.widget.TextView; importandroid.widget.Toast; importcom.chinaztt.fda.event.TestEventFirst; importcom.chinaztt.fda.ui.R; importcom.chinaztt.fda.ui.base.BaseActivity; importcom.chinaztt.fda.utils.Log; importorg.androidannotations.annotations.Click; importorg.androidannotations.annotations.EActivity; importorg.androidannotations.annotations.ViewById; importorg.w3c.dom.Text; importde.greenrobot.event.EventBus; /** * 當前類注釋:EventBus組件間數據通信實例 * 項目名:FastDev4Android * 包名:com.chinaztt.fda.test * 作者:江清清 on 15/11/3 13:14 * 郵箱:jiangqqlmj@163.com * QQ: 781931404 * 公司:江蘇中天科技軟件技術有限公司 */ @EActivity public classEventBusTestActivity extendsBaseActivity{ Button button_one; TextView textView_one; @Override protected void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.event_bus_test_layout); EventBus.getDefault().register(this); button_one=(Button)this.findViewById(R.id.button_one); textView_one=(TextView)this.findViewById(R.id.textView_one); button_one.setOnClickListener(newView.OnClickListener() { @Override public void onClick(View v) { openActivity(EventBusTestTwoActivity_.class); } }); } /** * 收到消息 進行相關處理 * @param event */ public voidonEventMainThread(TestEventFirst event) { textView_one.setText(event.getMsg()); showToastMsgShort(event.getMsg()); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } } ~~~ ~~~ packagecom.chinaztt.fda.test; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.Button; importcom.chinaztt.fda.event.TestEventFirst; importcom.chinaztt.fda.ui.R; importcom.chinaztt.fda.ui.base.BaseActivity; importorg.androidannotations.annotations.Click; importorg.androidannotations.annotations.EActivity; importorg.androidannotations.annotations.ViewById; importde.greenrobot.event.EventBus; /** * 當前類注釋: * 項目名:FastDev4Android * 包名:com.chinaztt.fda.test * 作者:江清清 on 15/11/3 14:25 * 郵箱:jiangqqlmj@163.com * QQ: 781931404 * 公司:江蘇中天科技軟件技術有限公司 */ @EActivity public classEventBusTestTwoActivity extends BaseActivity { Button button_two; @Override protected void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.event_bus_test_two_layout); button_two=(Button)this.findViewById(R.id.button_two); button_two.setOnClickListener(newView.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().post(new TestEventFirst("我是第二個Activity回傳的信息....")); EventBusTestTwoActivity.this.finish(); } }); } } ~~~ 到此我們的EventBus的基本使用已經講完了,看一下上面的效果演示,具體深入詳解以及其他的幾個方法的介紹和相關源代碼分析會在下一篇文章中進行講解。 我們的項目已經配置集成了消息總線EventBus的例子.歡迎大家去Github站點進行clone或者下載瀏覽:[https://github.com/jiangqqlmj/FastDev4Android](https://github.com/jiangqqlmj/FastDev4Android)?同時歡迎大家star和fork整個開源快速開發框架項目~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看