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>
<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
- Android实战简易教程-第五十九枪(EventBus小实例-传值、控制其他页控件显示)
- Android实战简易教程<五十九>(EventBus小实例-传值、控制其他页控件显示)
- EventBus小实例-传值、控制其他页控件显示
- Android实战简易教程-第五十八枪(AlarmManager类用法研究小实例)
- Android实战简易教程-第六十九枪(自定义控件实现雪花飘落效果)
- Android实战简易教程-第六十九枪(自定义控件实现雪花飘落效果)
- Android实战简易教程<十九>(SwipeRefreshLayout下拉刷新使用实例)
- Android实战简易教程-第五十一枪(ListView实现子控件的动态显示和隐藏、checkbox全选和反选)
- Android实战简易教程-第五枪(ListView用法研究)
- Android实战简易教程-第二十九枪(基于Face++实现年龄识别APP(一))
- Android实战简易教程-第四十九枪(两种方式实现网络图片异步加载)
- Android实战简易教程-第八枪(ImageSwitcher用法实例)
- Android实战简易教程-第一枪(Spinner控件详解)
- Android实战简易教程-第三十九枪(第三方短信验证平台Mob和验证码自动填入功能结合实例)
- Android简易实战教程--第四十九话《满屏拖动的控件》
- Android实战简易教程<五十八>(AlarmManager类用法研究小实例)
- Android简易实战教程--第二十九话《创建图片副本》
- Android简易实战教程--第三十九话《Chronometer实现倒计时》
- 网络连接之-xUtils框架(第三方通信框架)介绍(一)
- Python:安装setuptools,安装MySQLdb模块,连接数据库代码
- Java进阶之路 异常
- ANSI和UNICODE
- 第二周实践项目(3)~体验复杂度
- Android实战简易教程-第五十九枪(EventBus小实例-传值、控制其他页控件显示)
- RB-tree (红黑树)相关问题
- xHTML究竟有多少个标签?
- linux 可视化的几种模式
- HDU 1950 Bridging signals (DP动态规划 + 二分搜索 O(nlogn) )
- PathVariable
- 巧用ViewInject
- JVM和垃圾回收机制的一些整理
- 第三周项目3程序的多文件组织