EventBus 学习DEMO

使用一个登陆的成功后,关闭登陆界面,用户名和密码返回主页面的例子进行学习。
1 集成
build.gradle中配置:

  compile 'org.greenrobot:eventbus:3.0.0'

2 封装一个消息类,用于传递消息

package com.eventbus;

/**
 * Created by c9736 on 2017/3/5.
 */

public class LoginSeccessEvent {
    String msg;
    String username;
    String password;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

3 主界面用于接收消息并做相应操作

package com.eventbus;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class MainActivity extends AppCompatActivity {

    final static  String TAG="MAIN_ACTIVITY";
    Button button;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EventBus.getDefault().register(this);//在当前界面注册一个订阅者

        button= (Button) findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i=new Intent(getApplication(),ActivityLogin.class);
                startActivity(i);

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



    }

        @Subscribe //订阅事件注解 表示当接收接收到消息时运行此方法
    public void onEventMainThread(LoginSeccessEvent event){
        String msg=event.getMsg();  //通过event对象获取传递过来信息
        String password=event.getPassword();
        String username=event.getUsername();

        textView.setText(msg);

        Toast.makeText(getApplicationContext(),username+"/"+password,Toast.LENGTH_LONG).show();


    }
    @Subscribe
    public void onEventAsync(LoginSeccessEvent event){
        Log.d(TAG,"onEventAsync");
    }

    @Subscribe
    public void onEventBackground(LoginSeccessEvent event){
        Log.d(TAG,"onEventBackground");
    }

    @Subscribe
    public void onEvent(LoginSeccessEvent event){
        Log.d(TAG,"onEvent");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//销毁时取消注册,不再接收消息
    }
}

3 登陆界面获取登陆信息并且发送登陆信息

package com.eventbus;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.greenrobot.eventbus.EventBus;

public class ActivityLogin extends AppCompatActivity {

    EditText et_passowd;
    EditText et_username;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        et_passowd= (EditText) findViewById(R.id.et_password);
        et_username= (EditText) findViewById(R.id.et_username);

        button= (Button) findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginSeccessEvent event = new LoginSeccessEvent();
                event.setMsg("登录成功");
                event.setUsername(et_username.getText().toString());
                event.setPassword(et_passowd.getText().toString());
                EventBus.getDefault().post(event);//此处发送

                close();
            }
        });

    }
    public void  close(){
        this.finish();
    }
}

以下是布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/activity_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_margin="10dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:text="用户名"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:id="@+id/et_username"
            android:layout_width="match_parent"
            android:layout_height="50dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_margin="10dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:text="密码"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="50dp" />
    </LinearLayout>
    <Button
        android:text="login"
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

以下是Activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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.eventbus.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:id="@+id/btn"
        android:text="去登录"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv"
        android:layout_alignParentStart="true"
        android:layout_marginTop="20dp" />
</RelativeLayout>

/*
onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,
也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在
UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在
子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章