一,导入依赖
compile 'org.greenrobot:eventbus:3.1.1'
这里用到一个控件
TextInputLayout
,可以使页面变得美观,也需要导入依赖
compile 'com.android.support:design:23.3.0'
二,布局文件
1,第一个页面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.5" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="Welcome" android:textSize="30sp" android:textColor="#333333"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.5" android:orientation="vertical"> <android.support.design.widget.TextInputLayout android:id="@+id/usernameWrapper" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:hint="Username"/> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/passwordWrapper" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/usernameWrapper" android:layout_marginTop="4dp"> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="Password"/> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/btn" android:layout_marginTop="4dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login"/> </LinearLayout> </LinearLayout>2,第二个页面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_second" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center"> <Button android:id="@+id/button" android:layout_width="100dp" android:layout_height="match_parent" android:text="接收数据" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/uname" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/upass" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
三,定义消息事件类,有几个参数写几个参数
public class MenegerEvent { //定义消息事件类 public final String name; public final String pwd; public MenegerEvent(String name, String pwd) { this.name = name; this.pwd = pwd; } }
四,Activity
//注册和取消订阅事件 public class Main2Activity extends AppCompatActivity { private TextView name,pass; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //找到控件 name= findViewById(R.id.uname); pass= findViewById(R.id.upass); button= findViewById(R.id.button); //点击事件 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //注册EventBus EventBus.getDefault().register(Main2Activity.this); } }); } //事件订阅者处理事件 @Subscribe(threadMode = ThreadMode.POSTING,sticky = true) public void onUserEvent(MenegerEvent event) { name.setText("用户名:" + event.name); pass.setText("用户名:" + event.pwd); } //取消注册 @Override protected void onDestroy() { EventBus.getDefault().unregister(this); super.onDestroy(); } }
五,Avtivity2
//注册和取消订阅事件 public class Main2Activity extends AppCompatActivity { private TextView name,pass; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //找到控件 name= findViewById(R.id.uname); pass= findViewById(R.id.upass); button= findViewById(R.id.button); //点击事件 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //注册EventBus EventBus.getDefault().register(Main2Activity.this); } }); } //事件订阅者处理事件 @Subscribe(threadMode = ThreadMode.POSTING,sticky = true) public void onUserEvent(MenegerEvent event) { name.setText("用户名:" + event.name); pass.setText("用户名:" + event.pwd); } //取消注册 @Override protected void onDestroy() { EventBus.getDefault().unregister(this); super.onDestroy(); } }