页面之间的传值,有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()中取消注册:
@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>
<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不见了。我们可以想象一下,这种机制可以用在什么地方?
在设置页面控制某一个页面某个控件的显示?是不是类似广播的效果?
喜欢的朋友请关注我!谢谢您的支持!~