Android Intent (II)

Source: Internet
Author: User

The previous (Android intent (i)) explained a (it is used to adjust the B interface, which first understood as the parent interface) can be to B (here first understood as a sub-interface), this is a reverse process, that is, b to a pass data.

One: interface design
①,a (Activity_main.xml)
Important CONTROLS:
1 ' (TextView) Txt_infos: Used to display information transmitted from the B interface, the default value is xxxxxx
2 ' (Button) Btn_go_a: Used to jump to the B interface
Such as:

②, B (activity_b2.xml)
Important controls
1 ' (EditText) In_name: Used to enable the user to enter text
2 ' (Radiogroup) Rg_sex: The radio Group is used to select gender
2-1 ' (RadioButton) R_male: Male
2-2 ' (RadioButton) R_famale: Female
3 ' (button) BTN_OK: Close button (send message to a interface)
Such as:

Two: Code design
①,a:

Package Com.example.kayer.aas;import Android.content.intent;import android.support.v7.app.AppCompatActivity; Import Android.os.bundle;import android.view.view;import android.widget.button;import Android.widget.TextView;    Import Android.widget.toast;public class Mainactivity extends Appcompatactivity {private TextView Txt_infos = null;    Private Button btn_go_a = null;    Final int req_get_result_4_b = 100;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    This.init ();        } private void Init () {This.txt_infos = (TextView) Findviewbyid (R.id.txt_infos);        This.btn_go_a = (Button) Findviewbyid (r.id.btn_go_a); This.btn_go_A.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (View v)                {Intent $goto = new Intent (mainactivity.this, B2activity.class); Startactivityforresult ($gotO, MainActivity.this.REQ_GET_RESULT_4_B);    }        }); } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (Requestcode = = this.                 Req_get_result_4_b) {if (ResultCode = = RESULT_OK) {String $name = Data.getstringextra ("name");                Boolean $sex = Data.getbooleanextra ("Sex", false); This.txt_infos.setText ("Name:" + $name + "Gender:" + ($sex? ")            Male ":" female ")); }        }    }}

Attention:
1 ' final int req_get_result_4_b = 100; As the request code (you need to define it yourself)
2 ' RESULT_OK result code, no need to define yourself

②: B

Package Com.example.kayer.aas;import Android.content.intent;import android.support.v7.app.AppCompatActivity; Import Android.os.bundle;import android.view.view;import android.widget.button;import android.widget.EditText; Import Android.widget.radiobutton;import Android.widget.radiogroup;import Android.widget.textview;import    Android.widget.toast;public class B2activity extends Appcompatactivity {private EditText in_name = null;    Private Radiogroup rg_sex = null;    Private RadioButton R_male = null;    Private RadioButton R_female = null;    Private Button BTN_OK = null;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.LAYOUT.ACTIVITY_B2);    This.init ();        } private void Init () {this.in_name = (EditText) Findviewbyid (r.id.in_name);        This.rg_sex = (radiogroup) Findviewbyid (r.id.rg_sex);        This.r_male = (RadioButton) Findviewbyid (R.id.r_male); This.r_female = (RadiobuttON) Findviewbyid (R.id.r_female);        THIS.BTN_OK = (Button) Findviewbyid (R.ID.BTN_OK);                This.btn_ok.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (View v) {                String $name = B2Activity.this.in_name.getText (). toString (). Trim ();                    if ($name. Length () > 0) {boolean male = B2Activity.this.r_male.isChecked ();                    Intent $result = new Intent ();                    $result. PutExtra ("name", $name);                    $result. PutExtra ("Sex", male);                    B2Activity.this.setResult (RESULT_OK, $result);                B2Activity.this.finish (); }else{Toast.maketext (b2activity.this, "Please fill in the name!!!                ", Toast.length_short). Show ();        }            }        }); This.rg_sex.setOnCheckedChangeListener (New Radiogroup.oncheckedchangelistener () {@Override public V OID OnCheckedChanged (radiogroup Group, int checkedid) {String $sex;                if (B2Activity.this.r_male.getId () = = Checkedid) {$sex = "male";                }else{$sex = "female";            } toast.maketext (B2activity.this, "the gender of your choice:" + $sex, Toast.length_short). Show ();    }        }); }}

Android Intent (ii)

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.