注意:用几个对象类就封装就封装几个对象类
依赖
compile 'org.greenrobot:eventbus:3.0.0'
MainActivity中
public class MainActivity extends AppCompatActivity {
private EditText userr,pass;
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userr= (EditText) findViewById(R.id.user);
pass= (EditText) findViewById(R.id.pass);
login= (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {//发送黏性事件
@Override
public void onClick(View view) {
String s = userr.getText().toString();
String s1 =pass.getText().toString();
EventBus.getDefault().postSticky(new User(s,s1));
finish();
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
}
}
SecondActivity
public class SecondActivity extends AppCompatActivity {
private Button huoqu;
private TextView shu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
huoqu= (Button) findViewById(R.id.huoqu);
shu= (TextView) findViewById(R.id.shu);
huoqu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//注册事件
EventBus.getDefault().register(SecondActivity.this);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);//取消注册
}
@Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
public void getData(User user){
shu.setText("用户名:"+user.getUsername()+"密码:"+user.getPassword());
}
}
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.bwie.chentong.MainActivity">
<ImageView
android:id="@+id/image"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/user"
android:hint="请输入用户名"
android:layout_width="400dp"
android:layout_below="@id/image"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/pass"
android:hint="请输入密码"
android:layout_width="400dp"
android:layout_below="@id/user"
android:layout_height="wrap_content" />
<Button
android:id="@+id/login"
android:text="登录"
android:layout_below="@id/pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
User(对象类)
/**
* Created by chentong on 2017/11/7.
*/
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
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.bwie.chentong.SecondActivity">
<Button
android:id="@+id/huoqu"
android:text="获取数据"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/shu"
android:layout_below="@id/huoqu"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>