Givemepass's Android 惡補筆記

如何使用EventBus

| Comments

最近讀書會的同伴推薦了一套類似觀察者模式的套件
https://github.com/greenrobot/EventBus

簡單操作一下, 發現非常容易使用。

在Android Studio的gradle加入

 compile 'de.greenrobot:eventbus:2.4.0'

首先先宣告一個操作用的類別

public class MyEvent {
    private String myEventString;

    public String getMyEventString() {
        return myEventString;
    }

    public void setMyEventString(String myEventString) {
        this.myEventString = myEventString;
    }
}

只有簡單的getter & setter

接著宣告一個簡單的dialog layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edit_text"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send to bus"
        android:id="@+id/send_button"/>
</LinearLayout>

畫面會長這樣
如果button按下去 就會送出event給EventBus


點下show dialog

輸入文字

接著是我們dialog的程式碼

public class SampleDialog extends Dialog{

    private Context context;

    private Button sendBtn;

    private EditText editText;

    private EventBus mEventBus;

    public SampleDialog(Context context) {
        super(context, android.R.style.Theme_Light);
        this.context = context;

        mEventBus = EventBus.getDefault();
        setContentView(R.layout.dialog_layout);
        sendBtn = (Button) findViewById(R.id.send_button);
        editText = (EditText) findViewById(R.id.edit_text);
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyEvent event = new MyEvent();
                event.setMyEventString(editText.getText().toString());
                mEventBus.post(event);
                dismiss();
            }
        });
    }
}

接著是我們的MainActivity

public class MainActivity extends Activity {

    private EventBus eventBus;

    private TextView textView;

    private Button mShowDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.text);
        mShowDialog = (Button) findViewById(R.id.show_btn);
        mShowDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new SampleDialog(MainActivity.this).show();
            }
        });
        eventBus = EventBus.getDefault();
        eventBus.register(this);
    }

    public void onEventMainThread(MyEvent event){
        textView.setText(event.getMyEventString());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        eventBus.unregister(this);
    }
}

在使用上非常簡單 一開始先註冊藍色字體的EventBus
接著寫了onEventMainThread這個方法
它是用來接收由EventBus所傳出來的物件,
當你註冊後 寫入onEventXXX()方法
EventBus會幫你找到這個方法 並且傳送你要的參數給它

所以就會看到以下的畫面

Android Studio 程式碼

Comments

comments powered by Disqus