Activity switching and data transfer

Source: Internet
Author: User

Use intent and bundle objects in antivity1 to switch activity and pass data

Button Button01 =(Button) Findviewbyid (R.id.button1); Button01.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {EditText et=(EditText) Findviewbyid (R.ID.EDITTEXT1); DoubleEditText1 = Double.parsedouble (Et.gettext (). toString ());//Take valueString Choose= ""; RadioButton Rg1= (RadioButton) Findviewbyid (R.id.radiobutton);//check box value                                           if(Rg1.isclickable ()) {Choose /c7>= "M"; }                                           ElseChoose = "F"; Intent Intent=NewIntent (); Intent.setclass (ChangeTest01. This, ChangeTest02.class);//The activity was changedBundle Bundle=NewBundle ();//data transfer for activityBundle.putdouble ("Edit1", EDITTEXT1);//Pass the data within the quotation marks, followed by the pass valueBundle.putstring ("Choose", choose); Intent.putextras (bundle); //assign the bundle object to Intert;StartActivity (Intent);//Call another activity                                       }                                    }        );

And the data in the bundle object is accepted in the Activity2

Bundle bundle = This. Getintent (). Getextras ();//get the Bundle object in intentString Choose= Bundle.getstring ("choose");//get the data from the bundle object        DoubleEdit1 = bundle.getdouble ("Edit1"); String Choosetext= ""; if(Choose.equals ("M") ) {Choosetext= "A"; }Elsechoosetext= "B"; TextView TextView1=(TextView) Findviewbyid (R.ID.TEXTVIEW1);        Textview1.settext (Choosetext); TextView TextView2=(TextView) Findviewbyid (R.ID.TEXTVIEW2); Textview2.settext (string.valueof (edit1)); //Double Turn string

It is important to note that there is more than one activity, so we need to decide the main program;

In the Androidmainfest.xml

Set up

        <activity            android:name= ". ChangeTest01 "            android:label=" @string/title_activity_change_test01 ">            <intent-filter>                <action android:name= "Android.intent.action.MAIN"/>                <category android:name= " Android.intent.category.LAUNCHER "/>            </intent-filter>        </activity>        <activity            android:name= ". ChangeTest02 "            android:label=" @string/title_activity_change_test02 ">        </activity>

changetest01 because entry point was added so start from it first

Unlike Esplice in Android studio, you don't need to add activity manually.

If necessary, just change the main program in Androidmanifest.

Activity switching and data transfer

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.