Android practice simple tutorial-59th gun (EventBus small instance-pass value, control other page controls display), androideventbus

Source: Internet
Author: User
Tags eventbus

Android practice simple tutorial-59th gun (EventBus small instance-pass value, control other page controls display), androideventbus

The android-based shoes all know the value transfer between pages. You can use Intent to transfer the value, but it is not easy to dynamically control the display of another page control, next we will introduce an easy-to-use framework-EventBus. We will introduce its usage through an instance (you need to introduce the jar package before using EventBus, And the jar package is downloaded from the source code ).

I. Introduction to EventBus

To use EventBus, follow these steps:

1. Create a new class: As a message class

/*****/Package com. example. eventbusdemo;/*** @ author yayun * @ since September 14, 2015 */public class TestEvent {private String mMsg; public TestEvent (String mMsg) {this. mMsg = mMsg;} public String getMsg () {return mMsg ;}}
2. Register with the onCreate () method:

EventBus. getDefault (). register (this); // register EventBus

3. Create a method to receive the pass value:

Public void onEventMainThread (TestEvent testEvent) {String mString = "received message" + testEvent. getMsg (); mTextView. setText (mString); mButton2.setVisibility (View. GONE );}

4. cancel registration in onDestory:

@ Overrideprotected void onDestroy () {super. onDestroy (); EventBus. getDefault (). unregister (this); // cancel annotation}

5. Send messages

EventBus. getDefault (). post (new TestEvent ("button2 disappears"); // send a message

The order is not sequential. Don't forget it.

Ii. Sample Code 1. The first page:

Package com. example. eventbusdemo; import com. ypy. eventbus. eventBus; import android. app. activity; import android. content. intent; import android. drm. drmManagerClient. onEventListener; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. widget. button; import android. widget. textView;/*** @ author yayun * @ since September 14, 2015 **/public class MainActivity extends Activity implements OnClickListener {private Button mButton1, mButton2; private TextView mTextView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); EventBus. getDefault (). register (this); // register EventBusinitViews ();}/*** method description ** @ author yayun * @ since September 14, 2015 */private void initViews () {mButton1 = (Button) findViewById (R. id. btn_main); mButton2 = (Button) findViewById (R. id. btn_main_2); mTextView = (TextView) findViewById (R. id. TV _main); mButton1.setOnClickListener (this);}/***** message receiving Method * @ author yayun * @ since September 14, 2015 * @ param testEvent */public void onEventMainThread (TestEvent testEvent) {String mString = "received message" + testEvent. getMsg (); mTextView. setText (mString); mButton2.setVisibility (View. GONE);}/** (non-Javadoc) ** @ see android. view. view. onClickListener # onClick (android. view. view) */@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_main: Intent intent = new Intent (MainActivity. this, Second. class); startActivity (intent); break; default: break;}/* (non-Javadoc) * @ see android. app. activity # onDestroy () */@ Overrideprotected void onDestroy () {super. onDestroy (); EventBus. getDefault (). unregister (this); // uncomment }}

Second page:
/*****/Package com. example. eventbusdemo; import com. ypy. eventbus. eventBus; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. widget. button;/*** @ author yayun * @ since September 14, 2015 **/public class Second extends Activity {private Button mButtonSecond;/* (non-Javadoc) * @ see android. app. activity # onCreate (android. OS. bundle) */@ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_second); mButtonSecond = (Button) findViewById (R. id. btn_second); mButtonSecond. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {EventBus. getDefault (). post (new TestEvent ("button2 disappears"); // send message }});}}

3. layout file on the first page:
<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.eventbusdemo.MainActivity" >    <Button        android:id="@+id/btn_main"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Main Button" />    <Button        android:id="@+id/btn_main_2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/btn_main"        android:text="Main Button2" />    <TextView        android:id="@+id/tv_main"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/btn_main_2"        android:text="Main TextView" /></RelativeLayout>

4. layout file on the second page:

<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.eventbusdemo.MainActivity" >    <Button        android:id="@+id/btn_second"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Second Button" /></RelativeLayout>

The running instance is as follows:


You can see that after clicking the Button on the first page, the value is passed to the first page, and the Button2 on the first page is gone. We can imagine where this mechanism can be used?

Control the display of a widget on a page on the settings page? Is it similar to the broadcast effect?


If you like me, please follow me! Thank you for your support !~

Source code download

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.