Android EventBus

Android EventBus

效果图中我们可以看到,我们是点击"接收数据"按钮后,跳转到登录页面,然后把登录的数据传回并显示出来,我把"接收数据"按钮写在了SecondActivity中,所以记得在权限里,改变一下首页面的打开(将.MainActivity中的代码,挪到.SecondActivity中)

[html] view plain copy
  1.  <activity android:name=".MainActivity">  
  2. </activity>  
  3.     <activity android:name=".SecondActivity">  
  4.         <intent-filter>  
  5.             <action android:name="android.intent.action.MAIN" />  
  6.   
  7.             <category android:name="android.intent.category.LAUNCHER" />  
  8.         </intent-filter>  
  9.   
  10.     </activity>  
  11. </application>  

首先是权限依赖导入

[html] view plain copy
  1. compile 'org.greenrobot:eventbus:3.0.0'  

1.创建SecondActivity类,并布局

activity_second布局(获取数据界面)


[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_second"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context="com.eightgroup.eventbas.SecondActivity">  
  12.     <Button  
  13.         android:id="@+id/btn_shou"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="接收数据"/>  
  17.   
  18.     <LinearLayout  
  19.         android:layout_marginTop="20dp"  
  20.         android:layout_below="@+id/btn_shou"  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:orientation="horizontal">  
  24.   
  25.         <TextView  
  26.             android:text="TextView"  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:id="@+id/name"  
  30.             android:layout_weight="1"/>  
  31.   
  32.         <TextView  
  33.             android:text="TextView"  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="wrap_content"  
  36.             android:id="@+id/pass"  
  37.             android:layout_weight="1"/>  
  38.     </LinearLayout>  
  39. </RelativeLayout>  

然后是activity_main的布局(登录界面)
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_main"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context="com.eightgroup.eventbas.MainActivity">  
  12.     <EditText  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:inputType="textPersonName"  
  16.         android:ems="10"  
  17.         android:id="@+id/editText"  
  18.         android:hint="请输入用户名"/>  
  19.   
  20.     <EditText  
  21.         android:layout_marginTop="10dp"  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:inputType="numberPassword"  
  25.         android:ems="10"  
  26.         android:layout_below="@+id/editText"  
  27.         android:id="@+id/editText2"  
  28.         android:hint="请输入密码"/>  
  29.   
  30.     <Button  
  31.         android:layout_marginTop="50dp"  
  32.         android:text="Button"  
  33.         android:layout_width="match_parent"  
  34.         android:layout_height="wrap_content"  
  35.         android:layout_below="@+id/editText2"  
  36.         android:id="@+id/btn_send" />  
  37. </RelativeLayout>  
创建一个MessageEvent外部类
[html] view plain copy
  1. public class MessageEvent {  
  2.     public final String name;  
  3.     public final String pass;  
  4.   
  5.     public MessageEvent(String name, String pass) {  
  6.         this.name = name;  
  7.         this.pass = pass;  
  8.     }  
  9. }  

*普通模式:点击跳转然后再次跳回来可以将第二个页面的值传回来(用户登录)

*粘性模式:直接跳转传值(一般这个用的比较多)

.SecondActivity类(普通模式

记住一定要写上注册EventBus

[html] view plain copy
  1. EventBus.getDefault().register(SecondActivity.this);  
完整代码(接收方)

[html] view plain copy
  1. public class SecondActivity extends AppCompatActivity {    
  2.     
  3.     private TextView name,pass;    
  4.     private Button button;    
  5.     
  6.     @Override    
  7.     protected void onCreate(Bundle savedInstanceState) {    
  8.         super.onCreate(savedInstanceState);    
  9.         setContentView(R.layout.activity_second);    
  10.     
  11.         //注册EventBus    
  12.         EventBus.getDefault().register(SecondActivity.this);    
  13.     
  14.         name = (TextView) findViewById(R.id.name);    
  15.         pass = (TextView) findViewById(R.id.pass);    
  16.         button = (Button) findViewById(R.id.btn_shou);    
  17.     
  18.         button.setOnClickListener(new View.OnClickListener() {    
  19.             @Override    
  20.             public void onClick(View view) {    
  21.                 startActivity(new Intent(SecondActivity.this, MainActivity.class));    
  22.             }    
  23.         });    
  24.     
  25.     }    
  26.     (粘性模式直接在@Subscribe后面添加(sticky = true)
  27.     @Subscribe    
  28.     public void onUserEvent(MessageEvent event) {    
  29.         name.setText("用户名:" + event.name);    
  30.         pass.setText("密码:" + event.pass);    
  31.     }    
  32.     
  33.     @Override    
  34.     protected void onDestroy() {    
  35.         //取消注册事件    
  36.         EventBus.getDefault().unregister(SecondActivity.this);    
  37.         super.onDestroy();    
  38.     }    
  39.     
  40. }    
MainActivity类(发送方)
[html] view plain copy
  1. public class MainActivity extends AppCompatActivity {    
  2.     
  3.     private Button button;    
  4.     private EditText name,pass;    
  5.     
  6.     @Override    
  7.     protected void onCreate(Bundle savedInstanceState) {    
  8.         super.onCreate(savedInstanceState);    
  9.         setContentView(R.layout.activity_main);    
  10.     
  11.         button = (Button) findViewById(R.id.btn_send);    
  12.         name = (EditText) findViewById(R.id.editText);    
  13.         pass = (EditText) findViewById(R.id.editText2);    
  14.     
  15.         button.setOnClickListener(new View.OnClickListener() {    
  16.             @Override    
  17.             public void onClick(View view) {    
  18.     
  19.                 String userName="";    
  20.                 String userPass="";    
  21.                 if(!TextUtils.isEmpty(name.getText().toString())){    
  22.                     userName = name.getText().toString();    
  23.                     userPass = pass.getText().toString();    
  24.                     MessageEvent event = new MessageEvent(userName,userPass);   
  25.    (粘性事件是post改为postSticky) 
  26.                     EventBus.getDefault().post(event);    
  27.                 }else {    
  28.                     //不再发送事件Event    
  29.                     Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show();    
  30.                 }    
  31.     
  32.                 finish();    
  33.             }    
  34.         });    
  35.     }    
  36.     
  37. }