Butter Knife与EventBus

package com.example.administrator.test1;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;


public class MActivity extends Activity {
   // @BindView(R.id.btn_try) Button btn;
    @BindView(R.id.tv)  TextView tvv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_m);
            EventBus.getDefault().register(this);
            //btn = (Button) findViewById(R.id.btn_try);
            //tv = (TextView)findViewById(R.id.tv);
            ButterKnife.bind(this);
//            btn.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent(getApplicationContext(),
//                            SecondActivity.class);
//                    startActivity(intent);
//                }
//            });
        }
    @OnClick(R.id.btn_try)
    public void btnClick(View view) {
            Log.d("111:","123");
        Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
                   startActivity(intent);
    }

        @Subscribe
        public void onEventMainThread(FirstEvent event) {
            String msg = "onEventMainThread收到了消息:" + event.getMsg();
            Log.d("harvic", msg);
            tvv.setText(msg);
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
        }

        @Override
        protected void onDestroy(){
            super.onDestroy();
            EventBus.getDefault().unregister(this);
        }

}

<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_try"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="btn_bty"
        android:onClick="btnClick"/>
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

</LinearLayout>

package com.example.administrator.test1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class SecondActivity extends AppCompatActivity {
    //private Button btn_FirstEvent;
    @BindView(R.id.btn_first_event)
    Button btn_FirstEvent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
           // btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);
//            btn_FirstEvent.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    EventBus.getDefault().post(
//                            new FirstEvent("FirstEvent btn clicked"));
//                }
//            });
        }
    @OnClick(R.id.btn_first_event)
    public void btnClick(View view) {
        EventBus.getDefault().post(
                new FirstEvent("FirstEvent btn clicked"));
    }


}



<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_first_event"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="First Event"
        android:onClick="btnClick"/>

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值