huangf 2015-05-17
<code class="language-java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
package com.sohu.eventbustest; /** * Created by yuliu207272 on 2015/5/15. */ public class MessageEvent { private String msg; public MessageEvent(String msg) { this.msg = msg; } public String getMsg(){ return msg; } }
<code class="language-java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
EventBus.getDefault().register(MainActivity.this);
<code class="language-java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
public void onEvent(MessageEvent event){ String msg = event.getMsg(); tvText.setText(msg); Log.e(Tag, msg); }
<code class="language-java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
public class SendMessageActivity extends Activity { private EditText etText; private Button btnSend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); etText = (EditText)findViewById(R.id.et_text); btnSend = (Button)findViewById(R.id.btn_send); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(!TextUtils.isEmpty(etText.getText())){ String sendMessage = etText.getText().toString(); EventBus.getDefault().post(new MessageEvent(sendMessage)); } } }); } }
注意:方法中接收的MessageEvent和后面post方法中MessageEvent必须要是一种类型才能接收到消息<code class="language-java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
public class SendMessageActivity extends Activity { private EditText etText; private Button btnSend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); etText = (EditText)findViewById(R.id.et_text); btnSend = (Button)findViewById(R.id.btn_send); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(!TextUtils.isEmpty(etText.getText())){ String sendMessage = etText.getText().toString(); EventBus.getDefault().post(new MessageEvent(sendMessage)); } } }); } }
<code class="language-java" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;">
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.sohu.eventbustest.SendMessageActivity"> <EditText android:id="@+id/et_text" android:layout_width="fill_parent" android:layout_height="60dip" /> <Button android:gravity="center" android:textSize="18sp" android:textColor="#000000" android:id="@+id/btn_send" android:layout_width="match_parent" android:layout_height="60dip" android:text="发送消息"/> </LinearLayout>