EventBus3.0.0的使用


1.添加依赖

compile 'org.greenrobot:eventbus:3.0.0'

2.自定义事件类,并提供构造方法以及get方法

public class SecondEvent {
    public String mMsg;


    public SecondEvent(String mMsg) {
        this.mMsg = mMsg;
    }


    public String getmMsg() {
        return mMsg;
    }
    
}


3.在需要接收消息的类中注册EventBus
//在需要接收消息的页面注册EventBus
EventBus.getDefault().register(this);

4.在需要接收消息的类中反注册EventBus
@Override
    protected void onDestroy() {
        super.onDestroy();
	//避免忘记,所以接着在onDestory()中反注册EventBus
        EventBus.getDefault().unregister(this);
    }


5.在要发送消息的类中发送消息

//发送消息到MainActivity
EventBus.getDefault().post(new SecondEvent("我发消息过来了"));

6.在要接收消息的类中接收消息
//接收从SecondActivity传递过来的消息

    //注意此处的EventBus是3.0.0需要加注释
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public  void  onEventMainThread(SecondEvent secondEvent){
        String msg="MainActivity收到了消息:"+secondEvent.getmMsg();
        tv.setText(msg);
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

经过这六步就大功告成了


下面贴上每个类的完整代码:

activity_main.xml

<LinearLayout 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:orientation="vertical">

    <Button
        android:id="@+id/btn_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="MainActivity Button"/>

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

activity_second.xml

<LinearLayout 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:orientation="vertical">


    <Button
        android:id="@+id/btn_secord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SecordButton Button"/>


</LinearLayout>

SecondEvent.java
public class SecondEvent { 
public String mMsg; 
public SecondEvent(String mMsg) { 
this.mMsg = mMsg; 
}
 public String getmMsg() { 
return mMsg; 
}
}



MainActivity.java

public class MainActivity extends AppCompatActivity {
    Button btn;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.在需要接收消息的页面注册EventBus
        EventBus.getDefault().register(this);

        tv = (TextView) findViewById(R.id.tv);

        btn = (Button) findViewById(R.id.btn_main);


        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                startActivity(intent);
            }
        });

    }

    //接收从SecondActivity传递过来的消息

    //注意此处的EventBus是3.0.0需要加注释
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onEventMainThread(SecondEvent secondEvent) {
        String msg = "MainActivity收到了消息:" + secondEvent.getmMsg();
        tv.setText(msg);
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }


    //2.避免忘记,所以接着反注册EventBus
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}

SecondActivity.java

public  class SecondActivity  extends AppCompatActivity{

    private Button btn_second;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        btn_second = (Button) findViewById(R.id.btn_secord);
        btn_second.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //3.发送消息到MainActivity
                EventBus.getDefault().post(new SecondEvent("我发消息过来了"));
            }
        });
    }
}


Demo下载地址:http://download.csdn.net/detail/k2514091675/9793736
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 中,你可以使用事件总线(Event Bus)来实现组件之间的通信。事件总线是一个空的 Vue 实例,可以用于触发和监听自定义事件。 首先,在你的项目中创建一个新的 Vue 实例作为事件总线: ```javascript // eventBus.js import Vue from 'vue'; export const eventBus = new Vue(); ``` 然后,在需要通信的组件中,你可以使用事件总线来触发事件或监听事件: ```javascript // ComponentA.vue import { eventBus } from './eventBus.js'; export default { methods: { sendMessage() { eventBus.$emit('message', 'Hello from Component A!'); } } } ``` ```javascript // ComponentB.vue import { eventBus } from './eventBus.js'; export default { data() { return { receivedMessage: '' }; }, mounted() { eventBus.$on('message', (message) => { this.receivedMessage = message; }); } } ``` 在上面的示例中,ComponentA 在方法中通过 `eventBus.$emit` 触发了名为 `message` 的自定义事件,并传递了消息内容。而 ComponentB 在 `mounted` 钩子函数中通过 `eventBus.$on` 监听了 `message` 事件,当事件触发时会执行回调函数并将消息内容赋值给 `receivedMessage`。 这样,ComponentA 和 ComponentB 之间就可以通过事件总线进行通信了。请确保在不需要通信时及时销毁事件监听,以避免内存泄漏。 注意:事件总线在组件之间是全局共享的,可以用于任意组件之间的通信。但是如果你的项目越来越复杂,建议考虑使用更好的状态管理方案,如 Vuex。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值