EventBus的两个Activity的传值

//===================依赖           
compile 'org.greenrobot:eventbus:3.0.0'


//=====================================Bean类

public class Bean {

    String   number;
    String   mm;


    public Bean(String number, String mm) {
        this.number = number;
        this.mm = mm;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getMm() {
        return mm;
    }

    public void setMm(String mm) {
        this.mm = mm;
    }

    @Override
    public String toString() {
        return "Bean{" +
                "number='" + number + '\'' +
                ", mm='" + mm + '\'' +
                '}';
    }
}


//========================================MainActivity类

public class MainActivity extends AppCompatActivity {

    //定义属性
    EditText    number,mm;
    Button      but_dl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);


        //获取控件
        but_dl= (Button) findViewById(R.id.but_dl);
        number= (EditText) findViewById(R.id.number);
        mm= (EditText) findViewById(R.id.mm);

        //跳转
        but_dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在点击的事件中 -->获取输入 的值
                String sr1=number.getText().toString();
                String sr2=mm.getText().toString();
                //实例化有参的bean类赋值
                final Bean bean=new Bean(sr1,sr2);
                //发送黏贴事件
                EventBus.getDefault().postSticky(bean);
                //跳转
                Intent   intent=new Intent(MainActivity.this,SecondActivity.class);
                //启动
                startActivity(intent);
            }
        });


    }

//===================================第二个Activity的类
public class SecondActivity extends AppCompatActivity {

    //定义属性
    TextView   tv1;
    TextView   tv2;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
       //获取控件id
        tv1= (TextView) findViewById(R.id.tv1);
        tv2= (TextView) findViewById(R.id.tv2);

        //注册
        EventBus.getDefault().register(this);
 }

 //传递的类   线程
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void eventBusReceive(Bean bean) {
        tv1.setText(bean.getNumber());
        tv2.setText(bean.getMm());
    }

    //销毁方法
    @Override
    protected void onDestroy() {
        super.onDestroy();
//        注销
        EventBus.getDefault().unregister(this);
    }


}
//============================================布局1
xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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"
    tools:context="com.example.administrator.cr_1107.MainActivity"
    android:layout_margin="20dp">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="126dp"
        android:scaleType="centerCrop"
        android:src="@drawable/qq"
        android:layout_gravity="center"/>

    <EditText
        android:id="@+id/number"
        android:hint="请输入账号"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_below="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="21dp" />

    <EditText
        android:id="@+id/mm"
        android:hint="请输入密码"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_below="@+id/number"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="25dp" />

    <Button
        android:id="@+id/but_dl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mm"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="23dp"
        android:text="登录" />


LinearLayout>
//=============================================布局2
xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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"
    tools:context="com.example.administrator.cr_1107.SecondActivity"
    android:layout_margin="10dp">

   <TextView
       android:id="@+id/tv1"
       android:text="78954623"
       android:textSize="25dp"
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:gravity="center"
       android:background="#6699ff"
       />


    <TextView
        android:id="@+id/tv2"
        android:text="78954623"
        android:textSize="25dp"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:background="#6633ff"
        />



LinearLayout>


 

 






你可能感兴趣的:(EventBus的两个Activity的传值)