eventbus(簡單傳值)
阿新 • • 發佈:2019-02-17
//佈局兩個
//activity_ main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"//activity_secondandroid:gravity="center" android:orientation="vertical" tools:context="com.example.eventbus.MainActivity"> <Button android:id="@+id/btn_try" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="btn_bty" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tv" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" tools:context="com.example.eventbus.SecondActivity"> <Button android:id="@+id/btn_first_event" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="點選傳值" /> </LinearLayout>
//MainActivity
package com.example.eventbus; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; public class MainActivity extends AppCompatActivity { Button btn; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(MainActivity.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 //傳送訊息是使用EventBus中的Post方法來實現傳送的,傳送過去的是我們新建的類的例項! Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); } //必須要加這句話 //我們使用EventBus中最常用的onEventMainThread()函式來接收訊息 @Subscribe(threadMode = ThreadMode.MAIN) 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); } }//FirstEvent
public class FirstEvent { private String mMsg; public FirstEvent(String msg) { // TODO Auto-generated constructor stub mMsg = msg; } public String getMsg() { return mMsg; } }//SecondActivity
public class SecondActivity extends AppCompatActivity { 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("我是Eventbus,你好!")); finish(); } }); } }//依賴
compile 'org.greenrobot:eventbus:3.0.0'