Android enables page jumps and data transfer and return

Source: Internet
Author: User
Tags gettext

1. Achieve the effect:

Original interface:---- transfer data----------> After filling in the data, click on the post-calculation interface-----return data-----> Click the Back button, go back to the previous page, still able to retain the previously held data

2. Implementation code:

A. Two layout files:

Activity_main.xml:

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout 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:orientation= "Vertical"
Tools:context= "Com.example.activity_return. Mainactivity ">
<textview
Android:textsize= "30SP"
android:layout_margintop= "30DP"
android:layout_marginleft= "50DP"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/title"/>
<linearlayout
android:layout_margintop= "30DP"
android:orientation= "Horizontal"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<textview
Android:textsize= "20SP"
android:text= "@string/sex"
android:layout_marginleft= "50DP"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<radiogroup
android:orientation= "Horizontal"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<radiobutton
Android:checked= "true"
android:layout_marginleft= "10DP"
Android:id= "@+id/rb1"
android:text= "@string/man"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<radiobutton
android:layout_marginleft= "30DP"
Android:id= "@+id/rb2"
android:text= "@string/woman"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</RadioGroup>
</LinearLayout>
<linearlayout
android:layout_margintop= "10DP"
android:layout_marginleft= "50DP"
android:orientation= "Horizontal"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<textview
android:text= "@string/height"
Android:textsize= "20SP"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<edittext
Android:id= "@+id/et"
android:layout_marginleft= "10DP"
android:background= "@drawable/bg_edittext"
Android:layout_width= "80DP"
android:layout_height= "Wrap_content"/>
<textview
android:layout_marginleft= "10DP"
android:text= "CM"
Android:textsize= "20SP"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>
<button
android:layout_margintop= "50DP"
android:layout_marginleft= "100DP"
Android:id= "@+id/bt"
android:text= "@string/calculate"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>

New_activity.xml file:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "vertical" android:layout_width= "match_parent"
android:layout_height= "Match_parent" >

<textview
Android:id= "@+id/text"
android:layout_marginleft= "50DP"
android:layout_margintop= "50DP"
Android:textsize= "20DP"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<button
Android:text= "Back"
android:layout_margintop= "20DP"
android:layout_marginleft= "50DP"
Android:id= "@+id/button1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>

B. Two x Activity.java files:
Mainactivity.java:
public class Mainactivity extends Appcompatactivity {
Private EditText et;
Private RadioButton Rb1;
Private RadioButton Rb2;
Private Button BT;
Private Double height;
Private String sex;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Instantiating a control
InitData ();
Implement jump
Jump ();
}
private void InitData () {
et= (EditText) Findviewbyid (r.id.et);
bt= (Button) Findviewbyid (R.ID.BT);
rb1= (RadioButton) Findviewbyid (R.ID.RB1);
Rb2= (RadioButton) Findviewbyid (R.ID.RB2);

}
private void Jump () {
Bt.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
String Str=et.gettext (). toString ();
//must pay attention to this step of judgment, because the user may not fill in the height of the submission, then this situation will cause the program to collapse
if (!str.equals (")) Height=double.parsedouble (Et.gettext (). toString ());
else{
Et.sethint ("Please enter your height");
Return
}
///Then in this case, we can set the default checked to true for a button in the layout file and then change it according to the user
if (rb1.ischecked ()) {
sex= "M";
}else{
sex= "F";
}
Intent intent=new Intent ();
Intent.setclass (Mainactivity.this,new_activity.class);
Using Bundles to access data
Bundle Bundle=new Bundle ();
Bundle.putdouble ("height", height);
Bundle.putstring ("Sex", sex);
Then pass the data in the bundle to intent to transmit the past
Intent.putextras (bundle);
Startactivityforresult (intent,0);
}
});
}
//Here is the setting to get the data returned from the second page, if we do not set this, we return to the page, then the data will be emptied
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (RESULTCODE==RESULT_OK) {//If it is the identity returned
Get Data
Bundle Bundle=data.getextras ();
Sex=bundle.getstring ("Sex");
Height=bundle.getdouble ("height");
Keep the previous data
if (Sex.equals ("M")) {
Rb1.setchecked (TRUE);
}else{
Rb2.setchecked (TRUE);
}
String str=height.tostring ();
Et.settext (str);
}
}
}
New_activity.java:
public class New_activity extends Activity {
Private TextView TextView;
Private String sex;
Private String Sextext;
Private Double height;
Private String weight;
Private button button;
Private Intent Intent;
Private bundle bundle;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.new_layout);
InitData ();
Set the data returned to the previous page
Setbackdata ();
}
private void InitData () {
textview= (TextView) Findviewbyid (R.id.text);
button= (Button) Findviewbyid (R.id.button1);
Get the data transferred from the last page put in intent.
Intent=this.getintent ();
Bundle=intent.getextras ();
Sex=bundle.getstring ("Sex");
Height=bundle.getdouble ("height");
if (Sex.equals ("M")) {
sextext= "Male";
}else{
sextext= "female";
}
Getweight ();
}
private void Getweight () {
if (Sex.equals ("M")) {
weight= (height-80) *0.7+ "";
}else{
weight= (height-70) *0.6+ "";
}
}
private void Setbackdata () {
Textview.settext ("You are a" +sextext+ "your height is" +height+ "cm \ \ Your standard weight is" +weight+ "kg");
Button.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Because we have put the transmitted data in the InitData in the intent, so we can use the intent directly.
Setresult (result_ok,intent);
Finish ();
}
});
}
}

Android enables page jumps and data transfer and return

Related Article

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.