EventBus使用入门——像使用handler那样使用EventBus
来源:互联网 发布:设置php执行时间 编辑:程序博客网 时间:2024/04/29 06:15
EventBus字面意思是事件总线,可以用于Activity之间,线程之间传递消息。EventBus使用起来十分简单,又达到了项目中类与类之间解耦的效果。EventBus可以看做是对Handler和Broadcast的优化。
引入EventBus
引入EventBus有两种方式,一种是通过在build.gradle文件中加上下面这句话:
compile 'org.greenrobot:eventbus:3.0.0'
或者下载jar包放到libs文件夹下。
jar包下载地址为 https://search.maven.org/remote_content?g=org.greenrobot&a=eventbus&v=LATEST
EventBus使用步骤总结如下:
1.定义Events
public static class MessageEvent { /* Additional fields if needed */ }
2.准备Subcribers(订阅人),声明并定义你的订阅方法,并指定一个线程模式(threadmode)。
订阅方法用于处理接收到的events。
3.发送events
EventBus.getDefault().post(new MessageEvent());
下面来看一个例子:
实现效果如下:
上图中输入框输入文字,点击Post,就可以将文字包装成消息发送出去。发送完了在接收消息的地方把文本解析出来。
订阅者解析出文本后弹出toast。
布局文件如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.gucheng.testeventbus.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入要发送的消息" android:id="@+id/input"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="post" android:id="@+id/btn1"/></LinearLayout>
定义一个Events类
package com.gucheng.testeventbus;/** * Created by lenovo on 2017/10/2. */public class MessageEvent { private String mMsg = ""; public String getMsg() { return mMsg; } public MessageEvent(String msg) { mMsg = msg; }}
准备Subcribers。
@Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent msgEvent) { String msg = msgEvent.getMsg(); Toast.makeText(this, "post message is " + msg, Toast.LENGTH_LONG).show(); }
发送EventsButton btn1 = (Button)findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText editText = (EditText)findViewById(R.id.input); inputMsg = editText.getText().toString(); EventBus.getDefault().post(new MessageEvent(inputMsg)); } });
完整的MainActivity.java文件如下:package com.gucheng.testeventbus;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;import org.greenrobot.eventbus.ThreadMode;public class MainActivity extends AppCompatActivity { String inputMsg = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1 = (Button)findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText editText = (EditText)findViewById(R.id.input); inputMsg = editText.getText().toString(); EventBus.getDefault().post(new MessageEvent(inputMsg)); } }); } @Override protected void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override protected void onStop() { super.onStop(); EventBus.getDefault().unregister(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent msgEvent) { String msg = msgEvent.getMsg(); Toast.makeText(this, "post message is " + msg, Toast.LENGTH_LONG).show(); }}
EventBus github官方地址如下:https://github.com/greenrobot/EventBus
本文中例子完整代码如下:
https://github.com/gucheng3116/testEventBus
阅读全文
0 0
- EventBus使用入门——像使用handler那样使用EventBus
- EventBus 使用
- EventBus使用
- eventbus使用
- EventBus使用
- EventBus---使用
- EventBus使用
- EventBus 使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- EventBus使用
- Python数据分析学习总结
- Java注解项目实战即模拟Hibenernate生成sql语句
- eclipse中tomcat能正常启动,但是浏览器访问不了tomcat首页 问题解决
- LeetCode-84-Largest Rectangle in Histogram 经典题目,单调栈
- 树算法小记
- EventBus使用入门——像使用handler那样使用EventBus
- java 中的instanceof
- https
- 三句话巧记23中设计模式
- 【BZOJ】3956 Count 单调栈+ST表
- C语言实验——转换字母(顺序结构)
- leetcode-month2-week5
- stack,queue,hashmap的遍历
- VMware Workstation安装linux