EventBus 3.0 简单粗暴实例

3 篇文章 0 订阅
1 篇文章 0 订阅

EventBus可以用在Activity与Fragment之间相互通信。不必要自定义接口去层层回调,当然回调接口也可以实现它们之间的通信。但是也比不上EventBus的简单粗暴,这里直接上代码。
一,布局

<LinearLayout 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"
    android:orientation="vertical"
    >
    <Button 
        android:id="@+id/btn_skip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转"
        />
    <TextView
        android:id="@+id/tv_main"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout 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"
    android:orientation="vertical"
    >
    <Button 
        android:id="@+id/btn_commit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        />
    <EditText
        android:id="@+id/ev_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="输入信息" />
</LinearLayout>

二.MainActivity

public class MainActivity extends Activity implements OnClickListener {
    private TextView maintv;
    private Button skipbuttonButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        skipbuttonButton = (Button) findViewById(R.id.btn_skip);
        skipbuttonButton.setOnClickListener(this);
        maintv = (TextView) findViewById(R.id.tv_main);
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnMainEvent(MessageEvent event){
        Toast.makeText(this,event.getMessageString(),1).show();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_skip:
            Intent mIntent = new Intent();
            mIntent.setClass(this, SecondActivity.class);
            startActivity(mIntent);
            break;
        default:
            break;
        }   
    }   
}

三.SecondActivity


public class SecondActivity extends Activity implements android.view.View.OnClickListener {

    private String eventbus;
    private EditText inputev;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Button commitButton = (Button) findViewById(R.id.btn_commit);
        inputev = (EditText) findViewById(R.id.ev_input);



        commitButton.setOnClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_commit:
            eventbus = inputev.getText().toString().trim();
            if(TextUtils.isEmpty(eventbus)){
                Toast.makeText(this,"密码不能为空",1).show();
            }else{
                EventBus.getDefault().post(new MessageEvent(eventbus));
            }
            break;
        default:
            break;
        }   
    }    
}

四.MessageEvent

public class MessageEvent {

    private String messageString;
    public MessageEvent(String msg){
        this.messageString = msg;
    }
    public String getMessageString(){
        return messageString;
    }
    public void setMessageString(String msg){
        this.messageString = msg;
    }
}

最后很重要的一点就是添加依赖:compile ‘de.greenrobot:eventbus:3.0.0-beta1’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的兄弟叫顺溜2011

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值