The value between the pages, the Android-based children's shoes will know, can pass the value of intent, but dynamic control of the display of another page control I'm afraid this is not good use, the following we introduce a more useful framework-eventbus, It is introduced using an instance (to introduce a jar package in order to use the Eventbus,jar package in the source download).
First, introduce Eventbus
Steps to use Eventbus:
1. Create a new class: as a message class
/** * */package com.example.eventbusdemo;/** * @author Yayun * @since September 14, 2015 * */public class TestEvent {Priva Te string mmsg;public testevent (string mmsg) {this.mmsg = mmsg;} Public String getmsg () {return mmsg;}}
2. In the OnCreate () method out of registration:
Eventbus.getdefault (). Register (this);//Register Eventbus
3. Create method Receive value:
public void Oneventmainthread (TestEvent testevent) {String mstring= "received message" +testevent.getmsg (); Mtextview.settext ( mstring); Mbutton2.setvisibility (View.gone);}
4. Cancel the registration in Ondestory ():
@Overrideprotected void OnDestroy () {Super.ondestroy (); Eventbus.getdefault (). Unregister (this);//Uncomment}
5. Send a message
Eventbus.getdefault (). Post (New TestEvent ("Button2 disappears"));//Send Message
Do not forget the order.
Second, example code 1. 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 Mainactivit Y 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_ma in); MButton2 = (Button) Findviewbyid (r.id.btn_main_2); Mtextview = (TextView) Findviewbyid (R.id.tv_main); Mbutton1.setonclicklistener (this);} /** * * Method of receiving Messages * @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 for 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:paddi ngbottom= "@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= "@+i D/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>
Run the following example:
You can see that after clicking the button on the first page, the value is passed to the first page, and the Button2 of the first page is missing. Can we imagine where this mechanism can be used?
Control the display of a control on a page in the settings page? Is it similar to the effect of broadcasting?
Like friends please pay attention to me! Thank you for your support! ~
SOURCE download
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Combat Simple Tutorial-59th gun (Eventbus Small instance-pass value, control other page control display)