Android_EventBus3.0


使用中出了个小BUG,每次退回去之后就会多一个监听,不清楚为啥,另外一个就是传递过去的数据没有设置到TextView上,我表示很伤心,有知道原因的小伙伴希望提及我一下,献血。。。
先看看图吧


1.添加依赖

   compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile files('libs/butterknife-7.0.0.jar')
    compile 'com.android.support:design:24.2.0'
    compile 'org.greenrobot:eventbus:3.0.0'
  1. 布局文件
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/tv1"
        android:gravity="center" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/tv2"
        android:gravity="center"
        android:text="获取数据" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn"
        android:text="告诉老婆子,刚写了啥"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/tiet_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="年纪大了容易忘事" />
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:id="@+id/tiet_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="刚写的什么又忘记了" />
    </android.support.design.widget.TextInputLayout>
</LinearLayout>

  1. 实现接收数据的MainActivity
package com.oblivion.appbarlayout;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends FragmentActivity {


    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.btn2)
    Button btn2;
    private EventBus eventBus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_econd);
        ButterKnife.bind(this);
    }

    /**
     * 可见的时候注册
     */
    @Override
    protected void onResume() {
        super.onResume();
        eventBus = EventBus.getDefault();
        eventBus.register(this);
    }

    /**
     * 不可见,取消
     */
    @Override
    protected void onRestart() {
        super.onRestart();
        eventBus.unregister(this);
//        if(eventBus.isRegistered(this)){
//            eventBus.unregister(this);
//        }
    }

    @OnClick(R.id.btn2)
    public void onClick() {
        //进入SecondActivity
        startActivity(new Intent(MainActivity.this, SecondActivity.class));
        // finish();//如果不取消,将会注册多个?
    }

    @Subscribe(threadMode = ThreadMode.MAIN)//主线程
    public void getData(Entity entity) {//获取到数据,并设置数据
        Toast.makeText(MainActivity.this, "text1" + entity.text1, Toast.LENGTH_SHORT).show();
      //  System.out.println(entity.text1);
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        //取消EventBus
//        if (eventBus.isRegistered(this)) {
//            eventBus.unregister(this);
//        }
//    }
}

4.发送数据的SecondActivity

package com.oblivion.appbarlayout;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SecondActivity extends AppCompatActivity {

    @Bind(R.id.btn)
    Button btn;
    @Bind(R.id.tiet_1)
    TextInputLayout tiet1;
    @Bind(R.id.tiet_2)
    TextInputLayout tiet2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn)
    public void onClick() {
        String string1 = tiet1.getEditText().getText().toString().trim();
        String string2 = tiet2.getEditText().getText().toString().trim();
        Entity entity = new Entity();
        entity.text1 = string1;
        entity.text2 = string2;
        EventBus.getDefault().post(entity);
        startActivity(new Intent(SecondActivity.this, MainActivity.class));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值