效果图中我们可以看到,我们是点击"接收数据"按钮后,跳转到登录页面,然后把登录的数据传回并显示出来,我把"接收数据"按钮写在了SecondActivity中,所以记得在权限里,改变一下首页面的打开(将.MainActivity中的代码,挪到.SecondActivity中)
- <activity android:name=".MainActivity">
- </activity>
- <activity android:name=".SecondActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
首先是权限依赖导入
- compile 'org.greenrobot:eventbus:3.0.0'
1.创建SecondActivity类,并布局
activity_second布局(获取数据界面)
- <?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_second"
- 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.eightgroup.eventbas.SecondActivity">
- <Button
- android:id="@+id/btn_shou"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="接收数据"/>
- <LinearLayout
- android:layout_marginTop="20dp"
- android:layout_below="@+id/btn_shou"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <TextView
- android:text="TextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/name"
- android:layout_weight="1"/>
- <TextView
- android:text="TextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/pass"
- android:layout_weight="1"/>
- </LinearLayout>
- </RelativeLayout>
然后是activity_main的布局(登录界面)
- <?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.eightgroup.eventbas.MainActivity">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="textPersonName"
- android:ems="10"
- android:id="@+id/editText"
- android:hint="请输入用户名"/>
- <EditText
- android:layout_marginTop="10dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="numberPassword"
- android:ems="10"
- android:layout_below="@+id/editText"
- android:id="@+id/editText2"
- android:hint="请输入密码"/>
- <Button
- android:layout_marginTop="50dp"
- android:text="Button"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/editText2"
- android:id="@+id/btn_send" />
- </RelativeLayout>
- public class MessageEvent {
- public final String name;
- public final String pass;
- public MessageEvent(String name, String pass) {
- this.name = name;
- this.pass = pass;
- }
- }
*普通模式:点击跳转然后再次跳回来可以将第二个页面的值传回来(用户登录)
*粘性模式:直接跳转传值(一般这个用的比较多)
.SecondActivity类(普通模式)
记住一定要写上注册EventBus
- EventBus.getDefault().register(SecondActivity.this);
- public class SecondActivity extends AppCompatActivity {
- private TextView name,pass;
- private Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- //注册EventBus
- EventBus.getDefault().register(SecondActivity.this);
- name = (TextView) findViewById(R.id.name);
- pass = (TextView) findViewById(R.id.pass);
- button = (Button) findViewById(R.id.btn_shou);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- startActivity(new Intent(SecondActivity.this, MainActivity.class));
- }
- });
- }
- (粘性模式直接在@Subscribe后面添加(sticky = true))
- @Subscribe
- public void onUserEvent(MessageEvent event) {
- name.setText("用户名:" + event.name);
- pass.setText("密码:" + event.pass);
- }
- @Override
- protected void onDestroy() {
- //取消注册事件
- EventBus.getDefault().unregister(SecondActivity.this);
- super.onDestroy();
- }
- }
- public class MainActivity extends AppCompatActivity {
- private Button button;
- private EditText name,pass;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button) findViewById(R.id.btn_send);
- name = (EditText) findViewById(R.id.editText);
- pass = (EditText) findViewById(R.id.editText2);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- String userName="";
- String userPass="";
- if(!TextUtils.isEmpty(name.getText().toString())){
- userName = name.getText().toString();
- userPass = pass.getText().toString();
- MessageEvent event = new MessageEvent(userName,userPass);
- (粘性事件是post改为postSticky)
- EventBus.getDefault().post(event);
- }else {
- //不再发送事件Event
- Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
- }
- finish();
- }
- });
- }
- }