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>

你可能感兴趣的:(Butter Knife与EventBus)