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

来源:互联网 发布:手机淘宝没有实名认证 编辑:程序博客网 时间:2024/05/29 07:39

页面之间的传值,有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不见了。我们可以想象一下,这种机制可以用在什么地方?

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


喜欢的朋友请关注我!谢谢您的支持!~

源码下载

3 0
原创粉丝点击