宝思哲手表网站服务器维护中
web/
2025/9/27 6:40:01/
文章来源:
宝思哲手表网站,服务器维护中,专业app制作平台,杭州哪家seo公司好前言#xff1a;EventBus是上周项目中用到的#xff0c;网上的文章大都一样#xff0c;或者过时#xff0c;有用的没几篇#xff0c;经过琢磨#xff0c;请教他人#xff0c;也终于弄清楚点眉目#xff0c;记录下来分享给大家。 相关文章#xff1a; 1、《EventBus使用… 前言EventBus是上周项目中用到的网上的文章大都一样或者过时有用的没几篇经过琢磨请教他人也终于弄清楚点眉目记录下来分享给大家。 相关文章 1、《EventBus使用详解(一)——初步使用EventBus》 2、《EventBus使用详解(二)——EventBus使用进阶》 一、概述 EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在FragmentActivityService线程之间传递消息.优点是开销小代码更优雅。以及将发送者和接收者解耦。1、下载EventBus的类库源码https://github.com/greenrobot/EventBus 2、基本使用 1自定义一个类可以是空类比如 [java] view plaincopy public class AnyEventType { public AnyEventType(){} } 2在要接收消息的页面注册 [java] view plaincopy eventBus.register(this); 3发送消息 [java] view plaincopy eventBus.post(new AnyEventType event); 4接受消息的页面实现(共有四个函数各功能不同这是其中之一可以选择性的实现这里先实现一个) [java] view plaincopy public void onEvent(AnyEventType event) {} 5解除注册 [java] view plaincopy eventBus.unregister(this); 顺序就是这么个顺序可真正让自己写估计还是云里雾里的下面举个例子来说明下。 首先在EventBus中获取实例的方法一般是采用EventBus.getInstance()来获取默认的EventBus实例当然你也可以new一个又一个个人感觉还是用默认的比较好以防出错。 二、实战 先给大家看个例子 当击btn_try按钮的时候跳到第二个Activity当点击第二个activity上面的First Event按钮的时候向第一个Activity发送消息当第一个Activity收到消息后一方面将消息Toast显示一方面放入textView中显示。 按照下面的步骤下面来建这个工程 1、基本框架搭建 想必大家从一个Activity跳转到第二个Activity的程序应该都会写这里先稍稍把两个Activity跳转的代码建起来。后面再添加EventBus相关的玩意。 MainActivity布局activity_main.xml [html] view plaincopy LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:toolshttp://schemas.android.com/tools android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationvertical Button android:idid/btn_try android:layout_widthmatch_parent android:layout_heightwrap_content android:textbtn_bty/ TextView android:idid/tv android:layout_widthwrap_content android:layout_heightmatch_parent/ /LinearLayout 新建一个ActivitySecondActivity布局activity_second.xml [html] view plaincopy LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:toolshttp://schemas.android.com/tools android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationvertical tools:contextcom.harvic.try_eventbus_1.SecondActivity Button android:idid/btn_first_event android:layout_widthmatch_parent android:layout_heightwrap_content android:textFirst Event/ /LinearLayout MainActivity.java 点击btn跳转到第二个Activity [java] view plaincopy public class MainActivity extends Activity { Button btn; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn (Button) findViewById(R.id.btn_try); btn.setOnClickListener(new View.OnClickListener() { Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } } 到这基本框架就搭完了下面开始按步骤使用EventBus了。 2、新建一个类FirstEvent [java] view plaincopy package com.harvic.other; public class FirstEvent { private String mMsg; public FirstEvent(String msg) { // TODO Auto-generated constructor stub mMsg msg; } public String getMsg(){ return mMsg; } } 这个类很简单构造时传进去一个字符串然后可以通过getMsg()获取出来。 3、在要接收消息的页面注册EventBus 在上面的GIF图片的演示中大家也可以看到我们是要在MainActivity中接收发过来的消息的所以我们在MainActivity中注册消息。 通过我们会在OnCreate()函数中注册EventBus在OnDestroy函数中反注册。所以整体的注册与反注册的代码如下 [java] view plaincopy package com.example.tryeventbus_simple; import com.harvic.other.FirstEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Button btn; TextView tv; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册EventBus EventBus.getDefault().register(this); btn (Button) findViewById(R.id.btn_try); tv (TextView)findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } Override protected void onDestroy(){ super.onDestroy(); EventBus.getDefault().unregister(this);//反注册EventBus } } 4、发送消息 发送消息是使用EventBus中的Post方法来实现发送的发送过去的是我们新建的类的实例 [java] view plaincopy EventBus.getDefault().post(new FirstEvent(FirstEvent btn clicked)); 完整的SecondActivity.java的代码如下 [java] view plaincopy package com.example.tryeventbus_simple; import com.harvic.other.FirstEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class SecondActivity extends Activity { private Button btn_FirstEvent; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn_FirstEvent (Button) findViewById(R.id.btn_first_event); btn_FirstEvent.setOnClickListener(new View.OnClickListener() { Override public void onClick(View v) { // TODO Auto-generated method stub EventBus.getDefault().post( new FirstEvent(FirstEvent btn clicked)); } }); } } 5、接收消息 接收消息时我们使用EventBus中最常用的onEventMainThread函数来接收消息具体为什么用这个我们下篇再讲这里先给大家一个初步认识要先能把EventBus用起来先。 在MainActivity中重写onEventMainThreadFirstEvent event参数就是我们自己定义的类 在收到Event实例后我们将其中携带的消息取出一方面Toast出去一方面传到TextView中 [java] view plaincopy public void onEventMainThread(FirstEvent event) { String msg onEventMainThread收到了消息 event.getMsg(); Log.d(harvic, msg); tv.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } 完整的MainActiviy代码如下 [java] view plaincopy package com.example.tryeventbus_simple; import com.harvic.other.FirstEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Button btn; TextView tv; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); btn (Button) findViewById(R.id.btn_try); tv (TextView)findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } public void onEventMainThread(FirstEvent event) { String msg onEventMainThread收到了消息 event.getMsg(); Log.d(harvic, msg); tv.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } Override protected void onDestroy(){ super.onDestroy(); EventBus.getDefault().unregister(this); } } 好了到这基本上算初步把EventBus用起来了下篇再讲讲EventBus的几个函数及各个函数间是如何识别当前如何调用哪个函数的。 如果我的文章有帮到你请关注哦。 源码地址http://download.csdn.net/detail/harvic880925/8111357 请大家尊重原创者版权转载请标明出处http://blog.csdn.net/harvic880925/article/details/40660137 谢谢 转载于:https://www.cnblogs.com/Free-Thinker/p/7573450.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/82603.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!