The startactivityforresult method solves the problem of saving data between activities.

Source: Internet
Author: User

Res/layout/ex03_10.xml

<? XML version =" 1.0 "Encoding =" UTF-8 "?> <Absolutelayout xmlns: Android =" Http://schemas.android.com/apk/res/android "Android: Orientation =" Vertical "Android: layout_width =" Fill_parent "Android: layout_height =" Fill_parent "> <Radiogroup Android: layout_height =" Wrap_content "Android: layout_width =" Wrap_content "Android: Id =" @ + ID/rgsex "Android: Orientation =" Horizontal "Android: layout_x =" 100dip "Android: layout_y =" 60dip "> <Radiobutton Android: text =" @ String/sex_man "Android: layout_height =" Wrap_content "Android: layout_width =" Wrap_content "Android: checked =" True "Android: Id =" @ + ID/radio_man "> </Radiobutton> <radiobutton Android: text =" @ String/sex_woman "Android: layout_height =" Wrap_content "Android: layout_width =" Wrap_content "Android: Id =" @ + ID/radio_woman "> </Radiobutton> </radiogroup> <edittext Android: layout_height =" Wrap_content "Android: layout_width =" Wrap_content "Android: text =" Edittext "Android: Id =" @ + ID/etweight "Android: layout_x =" 97dip "Android: layout_y =" 133dip "> </Edittext> <button Android: text =" Button "Android: layout_height =" Wrap_content "Android: layout_width =" Wrap_content "Android: Id =" @ + ID/BTN "Android: layout_x =" 120dip "Android: layout_y =" 212dip "> </Button> </absolutelayout>

Res/layout/ex03_10_01.xml

 <? XML version = "1.0" encoding = "UTF-8" ?>  <  Absolutelayout   Xmlns : Android = Http://schemas.android.com/apk/res/android"    Android : Layout_width = "Fill_parent"    Android : Layout_height = "Fill_parent"  >      <  Textview   Android : Layout_height ="Wrap_content"             Android : Layout_width = "Wrap_content"             Android : Text = "Textview"             Android : Layout_x = "126dip"             Android : Layout_y = "144dip"             Android :ID = "@ + ID/tvresult"  >  </  Textview  >      <  Button   Android : Layout_width = "Wrap_content"       Android : Layout_x = "125dip"       Android :Layout_height = "Wrap_content"       Android : Layout_y = "248dip"       Android : ID = "@ + ID/btnbk"       Android : Text = "@ String/back"  >  </  Button  > </  Absolutelayout  > 
Src/ex03_10.java
Package gphone. ex03_10; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. edittext; import android. widget. radiobutton; import android. widget. radiogroup; public class ex03_10 extends activity {button BTN = NULL; edittext etweight = NULL; radiogroup rgsex = NULL;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. ex03_10); BTN = (button) findviewbyid (R. id. BTN); etweight = (edittext) findviewbyid (R. id. etweight); rgsex = (radiogroup) findviewbyid (R. id. rgsex); BTN. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubstring strweight = etweight. gettext (). tostring (); string strsex = "female"; if (rgsex. getcheckedradiobuttonid () = R. id. radio_man) {strsex = "male";} else {strsex = "female";} bundle B = new bundle (); B. putstring ("weight", strweight); B. putstring ("sex", strsex); intent = new intent (); intent. putextras (B); intent. setclass (ex03_10.this, ex03_10_01.class); startactivityforresult (inte

Src/ex03_10_02.java

 Package Gphone. ex03_10; Import Android. App. activity; Import Android. content. intent; Import Android. OS. Bundle; Import Android. View. view; Import Android. widget. Button; Import Android. widget. textview; Public   Class Ex03_10_01 Extends Activity {textview tvresult = Null ; Button btnbk = Null ; Intent = Null ; @ Override Protected   Void Oncreate (bundle savedinstancestate ){ // Todo auto-generated method stub  Super . Oncreate (savedinstancestate); setcontentview (R. layout. ex03_10_01); tvresult = (textview) findviewbyid (R. Id. tvresult); intent = This . Getintent (); bundle B = intent. getextras (); string strweight = B. getstring (" Weight "); String strsex = B. getstring (" Sex "); Tvresult. settext ("Result: "+ Strweight +" , "+ Strsex); btnbk = (button) findviewbyid (R. Id. btnbk); btnbk. setonclicklistener ( New Button. onclicklistener () {@ override Public   Void Onclick (view v ){ // Todo auto-generated method stub Ex03_10_01. This . Setresult (result_ OK, intent); ex03_10_01. This . Finish ();}});}}

Running result

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.