Android实战简易教程-第五十九枪(EventBus小实例-传值、控制其他页控件显示)
2015-09-14 17:08
197 查看
页面之间的传值,有android基础的童鞋都会知道,可以通过Intent进行传值,但是动态控制另一个页面控件的显示恐怕这个就不好用了吧,下面我们介绍一个比较好用的框架-EventBus,通过实例介绍它的使用(要引入jar包才能使用EventBus,jar包在源码下载中)。
一、介绍一下EventBus
使用EventBus的步骤:
1.新建一个类:作为消息类
3.创建方法接收传值:
4.在onDestory()中取消注册:
5.发送消息
顺序不分先后,不要忘记就好了。
第二个页面:
3.第一页的布局文件:
4.第二页的布局文件:
运行实例如下:
可以看到在点击了第一个页面的Button之后,将值传入到了第一个页面,且第一个页面的Button2不见了。我们可以想象一下,这种机制可以用在什么地方?
在设置页面控制某一个页面某个控件的显示?是不是类似广播的效果?
喜欢的朋友请关注我!谢谢您的支持!~
源码下载
一、介绍一下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()中取消注册:
@Override protected 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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);// 注册EventBus
initViews();
}
/**
* 方法说明
*
* @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)
*/
@Override
public 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()
*/
@Override protected 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) */ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_second); mButtonSecond=(Button) findViewById(R.id.btn_second); mButtonSecond.setOnClickListener(new OnClickListener() { @Override public 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不见了。我们可以想象一下,这种机制可以用在什么地方?
在设置页面控制某一个页面某个控件的显示?是不是类似广播的效果?
喜欢的朋友请关注我!谢谢您的支持!~
源码下载
相关文章推荐
- android各种服务链接
- Android SharedPreferences读取外部程序的SharedPreferences存储
- Android SharedPreferences的使用
- 开源中国 OsChina Android 客户端源码分析(9)下载APK功能
- Android Studio五--Gradle命令与第三方包
- Android Studio四--Gradle基础
- Android 自定义控件---SpreadView
- Android系统版本与API Level对照表
- Android Studio二--基本设置与运行
- Android Canvas绘图详解(图文)
- Android自定义圆形进度条
- Android Sqlite 导入外部数据库
- Android中的自定义圆形头像
- Android绘图深度解析
- Android(java)学习笔记247:ContentProvider使用之利用ContentProvider备份和还原手机短信(掌握)
- ADB+adbWireless 抛开USB调试,接入无线调试
- Error:Execution failed for task ':sample:dexDebug'. > com.android.ide.common.process.ProcessExceptio
- 如何一步步实现在Android平台下运用OpenGl
- Android SQLite使用
- Android Makefile 文件讲解