Android實戰簡易教程-第五十九槍(EventBus小執行個體-傳值、控制其他頁控制項顯示),androideventbus
頁面之間的傳值,有android基礎的童鞋都會知道,可以通過Intent進行傳值,但是動態控制另一個頁面控制項的顯示恐怕這個就不好用了吧,下面我們介紹一個比較好用的架構-EventBus,通過執行個體介紹它的使用(要引入jar包才能使用EventBus,jar包在源碼下載中)。
一、介紹一下EventBus
使用EventBus的步驟:
1.建立一個類:作為訊息類
/** * */package com.example.eventbusdemo;/** * @author yayun * @since 2015年9月14日 * */public class TestEvent {private String mMsg;public TestEvent(String mMsg) {this.mMsg = mMsg;}public String getMsg() {return mMsg;}}2.在onCreate()方法出註冊:
EventBus.getDefault().register(this);// 註冊EventBus
3.建立方法接收傳值:
public void onEventMainThread(TestEvent testEvent){String mString="收到訊息"+testEvent.getMsg();mTextView.setText(mString); mButton2.setVisibility(View.GONE);}
4.在onDestory()中取消註冊:
@Overrideprotected void onDestroy() {super.onDestroy();EventBus.getDefault().unregister(this);//取消注釋}
5.發送訊息
EventBus.getDefault().post(new TestEvent("button2消失"));//發送訊息
順序不分先後,不要忘記就好了。
二、範例程式碼1.第一個頁面:
package com.example.eventbusdemo;import com.ypy.eventbus.EventBus;import android.app.Activity;import android.content.Intent;import android.drm.DrmManagerClient.OnEventListener;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.TextView;/** * @author yayun * @since 2015年9月14日 * */public class MainActivity extends Activity implements OnClickListener {private Button mButton1, mButton2;private TextView mTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);EventBus.getDefault().register(this);// 註冊EventBusinitViews();}/** * 方法說明 * * @author yayun * @since 2015年9月14日 */private void initViews() {mButton1 = (Button) findViewById(R.id.btn_main);mButton2 = (Button) findViewById(R.id.btn_main_2);mTextView = (TextView) findViewById(R.id.tv_main);mButton1.setOnClickListener(this);}/** * * 接收訊息的方法 * @author yayun * @since 2015年9月14日 *@param testEvent */public void onEventMainThread(TestEvent testEvent){String mString="收到訊息"+testEvent.getMsg();mTextView.setText(mString); mButton2.setVisibility(View.GONE);}/* * (non-Javadoc) * * @see android.view.View.OnClickListener#onClick(android.view.View) */@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_main:Intent intent = new Intent(MainActivity.this, Second.class);startActivity(intent);break;default:break;}}/* (non-Javadoc) * @see android.app.Activity#onDestroy() */@Overrideprotected void onDestroy() {super.onDestroy();EventBus.getDefault().unregister(this);//取消注釋}}
第二個頁面:
/** * */package com.example.eventbusdemo;import com.ypy.eventbus.EventBus;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;/** * @author yayun * @since 2015年9月14日 * */public class Second extends Activity{private Button mButtonSecond;/* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_second);mButtonSecond=(Button) findViewById(R.id.btn_second);mButtonSecond.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {EventBus.getDefault().post(new TestEvent("button2消失"));//發送訊息}});}}
3.第一頁的布局檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.eventbusdemo.MainActivity" > <Button android:id="@+id/btn_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Main Button" /> <Button android:id="@+id/btn_main_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/btn_main" android:text="Main Button2" /> <TextView android:id="@+id/tv_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/btn_main_2" android:text="Main TextView" /></RelativeLayout>
4.第二頁的布局檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.eventbusdemo.MainActivity" > <Button android:id="@+id/btn_second" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Second Button" /></RelativeLayout>
運行執行個體如下:
可以看到在點擊了第一個頁面的Button之後,將值傳入到了第一個頁面,且第一個頁面的Button2不見了。我們可以想象一下,這種機制可以用在什麼地方?
在設定頁面控制某一個頁面某個控制項的顯示?是不是類似廣播的效果?
喜歡的朋友請關注我!謝謝您的支援!~
源碼下載
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。