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.
*/

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