Android实战简易教程<五十九>(EventBus小实例-传值、控制其他页控件显示)

来源:互联网 发布:淘宝四川正品小店卖假 编辑:程序博客网 时间:2024/06/05 10:58

页面之间的传值,有android基础的童鞋都会知道,可以通过Intent进行传值,但是动态控制另一个页面控件的显示恐怕这个就不好用了吧,下面我们介绍一个比较好用的框架-EventBus,通过实例介绍它的使用(要引入jar包才能使用EventBus,jar包在源码下载中)。

一、介绍一下EventBus

使用EventBus的步骤:

1.新建一个类:作为消息类

[java] view plaincopy
  1. <span style="font-size:12px;">/** 
  2.  *  
  3.  */  
  4. package com.example.eventbusdemo;  
  5.   
  6. /** 
  7.  * @author yayun 
  8.  * @since 2015年9月14日 
  9.  *  
  10.  */  
  11. public class TestEvent {  
  12.     private String mMsg;  
  13.   
  14.     public TestEvent(String mMsg) {  
  15.         this.mMsg = mMsg;  
  16.     }  
  17.   
  18.     public String getMsg() {  
  19.         return mMsg;  
  20.     }  
  21. }</span><span style="font-size:18px;">  
  22. </span>  
2.在onCreate()方法出注册:

[java] view plaincopy
  1. EventBus.getDefault().register(this);// 注册EventBus  

3.创建方法接收传值:

[java] view plaincopy
  1. public void onEventMainThread(TestEvent testEvent){  
  2.         String mString="收到消息"+testEvent.getMsg();  
  3.         mTextView.setText(mString);  
  4.         mButton2.setVisibility(View.GONE);  
  5.           
  6.     }  
4.在onDestory()中取消注册:

[java] view plaincopy
  1. @Override  
  2.     protected void onDestroy() {  
  3.         super.onDestroy();  
  4.         EventBus.getDefault().unregister(this);//取消注释  
  5.     }  
5.发送消息

[java] view plaincopy
  1. EventBus.getDefault().post(new TestEvent("button2消失"));//发送消息  
顺序不分先后,不要忘记就好了。

二、示例代码

1.第一个页面:
[java] view plaincopy
  1. package com.example.eventbusdemo;  
  2.   
  3. import com.ypy.eventbus.EventBus;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.drm.DrmManagerClient.OnEventListener;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.view.Window;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14.   
  15. /** 
  16.  * @author yayun 
  17.  * @since 2015年9月14日 
  18.  *  
  19.  */  
  20. public class MainActivity extends Activity implements OnClickListener {  
  21.     private Button mButton1, mButton2;  
  22.     private TextView mTextView;  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  28.         setContentView(R.layout.activity_main);  
  29.         EventBus.getDefault().register(this);// 注册EventBus  
  30.         initViews();  
  31.     }  
  32.   
  33.     /** 
  34.      * 方法说明 
  35.      *  
  36.      * @author yayun 
  37.      * @since 2015年9月14日 
  38.      */  
  39.     private void initViews() {  
  40.         mButton1 = (Button) findViewById(R.id.btn_main);  
  41.         mButton2 = (Button) findViewById(R.id.btn_main_2);  
  42.         mTextView = (TextView) findViewById(R.id.tv_main);  
  43.         mButton1.setOnClickListener(this);  
  44.   
  45.     }  
  46.     /** 
  47.      *  
  48.      * 接收消息的方法 
  49.      * @author yayun 
  50.      * @since 2015年9月14日 
  51.      *@param testEvent 
  52.      */  
  53.     public void onEventMainThread(TestEvent testEvent){  
  54.         String mString="收到消息"+testEvent.getMsg();  
  55.         mTextView.setText(mString);  
  56.         mButton2.setVisibility(View.GONE);  
  57.           
  58.     }  
  59.   
  60.     /* 
  61.      * (non-Javadoc) 
  62.      *  
  63.      * @see android.view.View.OnClickListener#onClick(android.view.View) 
  64.      */  
  65.     @Override  
  66.     public void onClick(View v) {  
  67.         switch (v.getId()) {  
  68.         case R.id.btn_main:  
  69.             Intent intent = new Intent(MainActivity.this, Second.class);  
  70.             startActivity(intent);  
  71.             break;  
  72.   
  73.         default:  
  74.             break;  
  75.         }  
  76.   
  77.     }  
  78.     /* (non-Javadoc) 
  79.      * @see android.app.Activity#onDestroy() 
  80.      */  
  81.     @Override  
  82.     protected void onDestroy() {  
  83.         super.onDestroy();  
  84.         EventBus.getDefault().unregister(this);//取消注释  
  85.     }  
  86.   
  87. }  
第二个页面:
[java] view plaincopy
  1. /** 
  2.  *  
  3.  */  
  4. package com.example.eventbusdemo;  
  5.   
  6. import com.ypy.eventbus.EventBus;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.view.Window;  
  13. import android.widget.Button;  
  14.   
  15. /** 
  16.  * @author yayun 
  17.  * @since 2015年9月14日 
  18.  *  
  19.  */  
  20. public class Second extends Activity{  
  21.     private Button mButtonSecond;  
  22.     /* (non-Javadoc) 
  23.      * @see android.app.Activity#onCreate(android.os.Bundle) 
  24.      */  
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         // TODO Auto-generated method stub  
  28.         super.onCreate(savedInstanceState);  
  29.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  30.         setContentView(R.layout.activity_second);  
  31.         mButtonSecond=(Button) findViewById(R.id.btn_second);  
  32.         mButtonSecond.setOnClickListener(new OnClickListener() {  
  33.               
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 EventBus.getDefault().post(new TestEvent("button2消失"));//发送消息  
  37.                   
  38.             }  
  39.         });  
  40.     }  
  41.   
  42. }  
3.第一页的布局文件:
[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context="com.example.eventbusdemo.MainActivity" >  
  10.   
  11.     <Button  
  12.         android:id="@+id/btn_main"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="Main Button" />  
  16.   
  17.     <Button  
  18.         android:id="@+id/btn_main_2"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_below="@+id/btn_main"  
  22.         android:text="Main Button2" />  
  23.   
  24.     <TextView  
  25.         android:id="@+id/tv_main"  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_below="@+id/btn_main_2"  
  29.         android:text="Main TextView" />  
  30.   
  31. </RelativeLayout>  
4.第二页的布局文件:

[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context="com.example.eventbusdemo.MainActivity" >  
  10.   
  11.     <Button  
  12.         android:id="@+id/btn_second"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="Second Button" />  
  16.   
  17. </RelativeLayout>  
运行实例如下:


可以看到在点击了第一个页面的Button之后,将值传入到了第一个页面,且第一个页面的Button2不见了。我们可以想象一下,这种机制可以用在什么地方?

在设置页面控制某一个页面某个控件的显示?是不是类似广播的效果?

源码下载

0 0
原创粉丝点击