Android 移动开发 | Intent 实现数据传递的方式&实现数据的回传

Android中Intent 实现数据传递的方式有多种,可以传递基本的数据类型的数据,这是最简单的,也可以通过序列化数据传递复杂的对象数据…这里只写其中的方式,还有其他方式没有全部写出来,拿前面自己写的小案例"改造"测试…

1. 具体的传递实现过程:

HeightForecastActivity:

package com.example.myapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class LYXForecastHieghtActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    
    
Button btn_date,btn_Intent,btn_Bundle,btn_Serializable;
RadioGroup rg_gender;
RadioButton rb_man,rb_wom;
LinearLayout pagebg;
TextView atv_forecastMsg,btv_forecastMsg;
CheckBox checkBox,checkBox2,checkBox3,checkBox4;
EditText edt_birthday,edt_fatherheight,edt_motherheight;
int choiceSex;
int childheight;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hieght_forecast);
        //获取布局中对应的控件的id
        btn_date=findViewById(R.id.btn_date);
        btn_date.setOnClickListener(this);
        pagebg=findViewById(R.id.pagebg);
        edt_fatherheight=findViewById(R.id.edt_fatherHeight);
        edt_motherheight=findViewById(R.id.edt_motherheight);
        edt_birthday=findViewById(R.id.edt_birthday);
        atv_forecastMsg=findViewById(R.id.atv_forecastMsg);
        btv_forecastMsg=findViewById(R.id.btv_forecastMsg);

        btn_Intent=findViewById(R.id.btn_Intent);
        btn_Intent.setOnClickListener(this);
        btn_Bundle=findViewById(R.id.btn_Bundle);
        btn_Bundle.setOnClickListener(this);
        btn_Serializable=findViewById(R.id.btn_Serializable);
        btn_Serializable.setOnClickListener(this);


        rg_gender=findViewById(R.id.rg_gender);
        rb_man=findViewById(R.id.rb_man);
        rb_man.setOnCheckedChangeListener(this);
        rb_wom=findViewById(R.id.rb_wom);
        rb_wom.setOnCheckedChangeListener(this);

        checkBox=findViewById(R.id.checkBox);
        checkBox2=findViewById(R.id.checkBox2);
        checkBox3=findViewById(R.id.checkBox3);
        checkBox4=findViewById(R.id.checkBox4);

        //设置性别单选按钮 监听事件
        rg_gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    
    
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
    
    
                if(checkedId==rb_man.getId()){
    
    
                    choiceSex=1;    //选择男
                }
                else{
    
    
                    choiceSex=0;    //选择女
                }
            }
        });

    }

    //性别选择 点击弹框
    public void onCheckedChanged(CompoundButton compoundButton,boolean isCheck){
    
    
        switch(compoundButton.getId()){
    
    
            case R.id.rb_man:
                if (isCheck){
    
    
                    Toast.makeText(this, "男", Toast.LENGTH_SHORT).show();
                    pagebg.setBackgroundColor(Color.argb(100,228,237,193));
                }
                break;
            case R.id.rb_wom:
                if (isCheck){
    
    
                    Toast.makeText(this, "女", Toast.LENGTH_SHORT).show();
                    pagebg.setBackgroundColor(Color.argb(100,193,226,143));
                }
                break;
            default:
                break;    
        }
    }

     // 按钮事件处理
    public void onClick(View view){
    
    
       switch(view.getId()){
    
    
           case R.id.btn_date:
               DatePickerDialog.OnDateSetListener birthdayListener=new DatePickerDialog.OnDateSetListener() {
    
    
                   @Override
                   public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    
    
                       month=month+1;
                       edt_birthday.setText(year+"-"+month+"-"+dayOfMonth);
                   }
               };
               Calendar calendar=Calendar.getInstance();
               int byear,bmonth,bday;
               byear=calendar.get(Calendar.YEAR)-20;
               bmonth=calendar.get(Calendar.MONTH);
               bday=calendar.get(Calendar.DAY_OF_MONTH);
               DatePickerDialog datePickerDialog=new DatePickerDialog(this,birthdayListener,byear,bmonth,bday);
               datePickerDialog.show();
               break;
           case R.id.btn_Intent:
               // 创建Intent ,明确源Activity和目标Activity
               Intent intent = new Intent(HeightForecastActivity.this,ResultIntentActivity.class);
               // 获取控件上的数据
               String str_birthday = edt_birthday.getText().toString();
               Boolean isMan = rb_man.isChecked();
               int fh = Integer.parseInt(edt_fatherheight.getText().toString());
               int mh = Integer.parseInt(edt_motherheight.getText().toString());
               Boolean ischeckBox = checkBox.isChecked();
               Boolean ischeckBox2 = checkBox2.isChecked();
               Boolean ischeckBox3 = checkBox3.isChecked();
               Boolean ischeckBox4 = checkBox4.isChecked();
               // 通过putExtra方法 在Intent上绑定数据
               //1. 第一种通过Intent直接传递数据:
               intent.putExtra("birthday",str_birthday);
               intent.putExtra("isMan",isMan);
               intent.putExtra("fh",fh);
               intent.putExtra("mh",mh);
               intent.putExtra("ischeckBox",ischeckBox);
               intent.putExtra("ischeckBox2",ischeckBox2);
               intent.putExtra("ischeckBox3",ischeckBox3);
               intent.putExtra("ischeckBox4",ischeckBox4);
               //最后通过startActivity方法实现Activity的跳转
               startActivity(intent);
               break;
           case R.id.btn_Bundle:
               // 创建Intent ,明确源Activity和目标Activity
               Intent intent2 = new Intent(HeightForecastActivity.this,ResultBundleActivity.class);
               // 获取控件上的数据
               String str_birthday2 = edt_birthday.getText().toString();
               Boolean isMan2 = rb_man.isChecked();
               int fh2 = Integer.parseInt(edt_fatherheight.getText().toString());
               int mh2 = Integer.parseInt(edt_motherheight.getText().toString());
               Boolean ischeckBox11 = checkBox.isChecked();
               Boolean ischeckBox22 = checkBox2.isChecked();
               Boolean ischeckBox23 = checkBox3.isChecked();
               Boolean ischeckBox24 = checkBox4.isChecked();

               //2. 第二种方法: Bundle传递数据,先通过putXXX方法把数据加载在Bundle中,然后再将bundle加载到intent 上
               Bundle bundle = new Bundle();
               bundle.putString("birthday",str_birthday2);
               bundle.putBoolean("isMan2",isMan2);
               bundle.putBoolean("ischeckBox11",ischeckBox11);
               bundle.putBoolean("ischeckBox22",ischeckBox22);
               bundle.putBoolean("ischeckBox23",ischeckBox23);
               bundle.putBoolean("ischeckBox24",ischeckBox24);
               bundle.putInt("fh2",fh2);
               bundle.putInt("mh2",mh2);
               intent2.putExtra("dataBundle",bundle);
               //最后通过startActivity方法实现Activity的跳转
               startActivity(intent2);
               break;
           case R.id.btn_Serializable:
               // 创建Intent ,明确源Activity和目标Activity
               Intent intent4 = new Intent(HeightForecastActivity.this,ResultSerializableActivity.class);
               // 获取控件上的数据
               String str_birthday4 = edt_birthday.getText().toString();
               Boolean isMan4 = rb_man.isChecked();
               int fh4 = Integer.parseInt(edt_fatherheight.getText().toString());
               int mh4 = Integer.parseInt(edt_motherheight.getText().toString());
               Boolean ischeckBox41 = checkBox2.isChecked();
               Boolean ischeckBox42 = checkBox3.isChecked();
               Boolean ischeckBox43 = checkBox4.isChecked();
               //第三种方法:传对象;
               FHInfo fhInfo=new FHInfo();
               fhInfo.setBirthday(str_birthday4);
               fhInfo.setMan(isMan4);
               fhInfo.setIscheckBox41(ischeckBox41);
               fhInfo.setIscheckBox42(ischeckBox42);
               fhInfo.setIscheckBox43(ischeckBox43);
               fhInfo.setFh(fh4);
               fhInfo.setMh(mh4);
               //只有序列化的对象才能通过intent进行传递
               intent4.putExtra("myInfo",fhInfo);
               //最后通过startActivity方法实现Activity的跳转
               startActivity(intent4);
               break;
       }
    }
}

xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/pagebg">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_name"
        android:gravity="center"
        android:textStyle="bold"
        android:textColor="#2D7FEE"
        android:layout_margin="30dp"
        android:textSize="24dp"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp">
        <TextView
            android:id="@+id/tv_birthday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tv_birthday"
            android:layout_marginTop="20dp"
            android:textSize="16dp"/>
        <EditText
            android:id="@+id/edt_birthday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="140dp"
            android:ems="10"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@id/tv_birthday"
            android:inputType="date"/>
        <Button
            android:id="@+id/btn_date"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:background="@drawable/button"
            android:shadowRadius="@android:integer/config_longAnimTime"
            android:text="@string/btn_date"
            android:layout_toRightOf="@id/edt_birthday"
            android:layout_marginLeft="15dp"
            android:textColor="#FFFFFF"/>
        <TextView
            android:id="@+id/tv_fatherheight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/father_height"
            android:layout_below="@id/tv_birthday"
            android:layout_marginTop="40dp"
            android:textSize="16dp"/>
        <EditText
            android:id="@+id/edt_fatherHeight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="12"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@id/tv_fatherheight"
            android:layout_marginTop="20dp"
            android:hint="@string/hint_text"
            android:layout_below="@id/tv_birthday"
            android:inputType="number"/>
        <TextView
            android:id="@+id/tv_motherheight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/mother_height"
            android:layout_below="@id/tv_fatherheight"
            android:layout_marginTop="30dp"
            android:textSize="16dp"/>
        <EditText
            android:id="@+id/edt_motherheight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="12"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@id/tv_fatherheight"
            android:layout_marginTop="10dp"
            android:hint="@string/hint_text"
            android:layout_below="@id/edt_fatherHeight"
            android:inputType="number"/>
        <RadioGroup
            android:id="@+id/rg_gender"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_motherheight"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tv_sex"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:textSize="16dp"
                android:text="@string/sex"/>
            <RadioButton
                android:id="@+id/rb_man"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"
                android:text="@string/gener_man"/>
            <RadioButton
                android:id="@+id/rb_wom"
                android:layout_width="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:layout_toRightOf="@id/rb_man"
                android:layout_height="wrap_content"
                android:text="@string/gener_wom"/>
        </RadioGroup>

        <LinearLayout
            android:id="@+id/ly_grow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rg_gender"
            android:layout_marginTop="15dp"
            android:orientation="horizontal">
            <CheckBox
                android:id="@+id/checkBox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="@string/checkBox1"/>
            <CheckBox
                android:id="@+id/checkBox2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="@string/checkBox2"/>
            <CheckBox
                android:id="@+id/checkBox3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="@string/checkBox3"/>
            <CheckBox
                android:id="@+id/checkBox4"
                android:layout_marginLeft="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/checkBox4"/>
        </LinearLayout>
        <Button
            android:id="@+id/btn_Intent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="通过Intent直接传递"
            android:textSize="18dp"
            android:background="@drawable/button"
            android:shadowRadius="@android:integer/config_longAnimTime"
            android:textColor="#FFFFFF"
            android:layout_below="@+id/ly_grow"/>
        <Button
            android:id="@+id/btn_Bundle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="通过Bundle传递"
            android:textSize="18dp"
            android:background="@drawable/button"
            android:shadowRadius="@android:integer/config_longAnimTime"
            android:textColor="#FFFFFF"
            android:layout_below="@+id/btn_Intent" />

        <Button
            android:id="@+id/btn_Serializable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Serializable序列化传递"
            android:textSize="18dp"
            android:background="@drawable/button"
            android:shadowRadius="@android:integer/config_longAnimTime"
            android:textColor="#FFFFFF"
            android:layout_below="@+id/btn_Bundle" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn_Serializable"
            android:id="@+id/atv_forecastMsg"/>

    </RelativeLayout>
</LinearLayout>

1.1 通过Intent直接传递简单的数据

在这里插入图片描述

ResultIntentActivity:

package com.example.myapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ResultIntentActivity extends AppCompatActivity {
    
    
TextView btv_forecastMsg;
Button btn_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result_intent);
        btn_result = findViewById(R.id.btn_result);
        btv_forecastMsg = findViewById(R.id.btv_forecastMsg);
        // 在目标Activity中通过 getIntent方法获取源Activity 传过来的Intent
        Intent intent = getIntent();

        btn_result = findViewById(R.id.btn_result);
        btv_forecastMsg = findViewById(R.id.btv_forecastMsg);
        // 在目标Activity中通过 getIntent方法获取源Activity 传过来的Intent
        Intent intent = getIntent();

        // 第一种方式 直接通过Intent 进行数据传递
        // getXXXExtra 方法直接获取控件上相应的数据
        
        String str_birthday = intent.getStringExtra("birthday");
        Boolean isMan = intent.getBooleanExtra("isMan",false);
        Boolean ischeckBox = intent.getBooleanExtra("ischeckBox",false);
        Boolean ischeckBox2 = intent.getBooleanExtra("ischeckBox2",false);
        Boolean ischeckBox3 = intent.getBooleanExtra("ischeckBox3",false);
        Boolean ischeckBox4 = intent.getBooleanExtra("ischeckBox4",false);
        int fh = intent.getIntExtra("fh",0);
        int mh = intent.getIntExtra("mh",0);

        btv_forecastMsg = findViewById(R.id.btv_forecastMsg);
        btv_forecastMsg.setText("你的生日为:"+str_birthday+"\n你是男生:"+isMan+"\n你爱运动:"+ischeckBox+"\n营养好 "+ischeckBox2+"\n爱喝牛奶"+ischeckBox3+"\n爱睡觉 "+ischeckBox4+"\n 你父亲的身高为:"+fh+"\n 你母亲的身高为:"+mh);
    }
}

1.2 通过Bundle打包数据传递简单的数据

在这里插入图片描述

ResultBundleActivity:

package com.example.myapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ResultBundleActivity extends AppCompatActivity {
    
    
TextView btv_forecastMsg;
Button btn_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result_bundle);
        btn_result = findViewById(R.id.btn_result);
        btv_forecastMsg = findViewById(R.id.btv_forecastMsg);
        // 在目标Activity中通过 getIntent方法获取源Activity 传过来的Intent
        Intent intent = getIntent();

        //第二种方式:先从intent上接收Bundle数据,再从Bundle里面解析各个数据
        Bundle bundle=intent.getBundleExtra("dataBundle");
        String str_birthday2=bundle.getString("birthday");
        Boolean isMan2 = intent.getBooleanExtra("isMan2",false);
        Boolean ischeckBox11 = intent.getBooleanExtra("ischeckBox11",false);
        Boolean ischeckBox22 = intent.getBooleanExtra("ischeckBox22",false);
        Boolean ischeckBox23 = intent.getBooleanExtra("ischeckBox23",false);
        Boolean ischeckBox24 = intent.getBooleanExtra("ischeckBox24",false);
        int fh2=bundle.getInt("fh2",0);
        int mh2=bundle.getInt("mh2",0);
        
        btv_forecastMsg = findViewById(R.id.btv_forecastMsg);
        btv_forecastMsg.setText("你的生日为:"+str_birthday2+"\n你是男生:"+isMan2+"\n你爱运动:"+ischeckBox22+"\n营养好 "+ischeckBox11+"\n爱喝牛奶"+ischeckBox23+"\n爱睡觉 "+ischeckBox24+"\n 你父亲的身高为:"+fh2+"\n 你母亲的身高为:"+mh2);

    }
}

1.3 通过Serializable接口序列化传递对象类型数据:

在这里插入图片描述

ResultSerializableActivity:

package com.example.myapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ResultSerializableActivity extends AppCompatActivity {
    
    
TextView btv_forecastMsg;
Button btn_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result_serializable);
        btn_result = findViewById(R.id.btn_result);
        btv_forecastMsg = findViewById(R.id.btv_forecastMsg);
        // 在目标Activity中通过 getIntent方法获取源Activity 传过来的Intent
        Intent intent = getIntent();

        // 第三种 :传对象
        FHInfo myinfo= (FHInfo) intent.getSerializableExtra("myInfo");
        String str_birthday4=myinfo.getBirthday();
        Boolean isMan4=myinfo.isMan();
        Boolean ischeckBox41=myinfo.isIscheckBox41();
        Boolean ischeckBox42=myinfo.isIscheckBox42();
        Boolean ischeckBox43=myinfo.isIscheckBox43();
        int fh4=myinfo.getFh();
        int mh4=myinfo.getMh();
        btv_forecastMsg = findViewById(R.id.btv_forecastMsg);
        btv_forecastMsg.setText("你的生日为:"+str_birthday4+"\n你是男生:"+isMan4+"\n营养好 "+ischeckBox41+"\n爱喝牛奶"+ischeckBox42+"\n爱睡觉 "+ischeckBox43+"\n 你父亲的身高为:"+fh4+"\n 你母亲的身高为:"+mh4);
        
    }
}

第三种需要先序列化数据,创建FHInfo.class:

package com.example.myapp2;
import java.io.Serializable;
public class FHInfo implements  Serializable {
    
    
    private String birthday;
    private boolean isMan;
    private boolean ischeckBox41;
    private boolean ischeckBox42;
    private boolean ischeckBox43;
    private int fh;
    private int mh;

    public String getBirthday() {
    
    
        return birthday;
    }

    public void setBirthday(String birthday) {
    
    
        this.birthday = birthday;
    }

    public boolean isMan() {
    
    
        return isMan;
    }

    public void setMan(boolean man) {
    
    
        isMan = man;
    }


    public boolean isIscheckBox41() {
    
    
        return ischeckBox41;
    }

    public void setIscheckBox41(boolean ischeckBox41) {
    
    
        this.ischeckBox41 = ischeckBox41;
    }

    public boolean isIscheckBox42() {
    
    
        return ischeckBox42;
    }

    public void setIscheckBox42(boolean ischeckBox42) {
    
    
        this.ischeckBox42 = ischeckBox42;
    }

    public boolean isIscheckBox43() {
    
    
        return ischeckBox43;
    }

    public void setIscheckBox43(boolean ischeckBox43) {
    
    
        this.ischeckBox43 = ischeckBox43;
    }

    public int getFh() {
    
    
        return fh;
    }

    public void setFh(int fh) {
    
    
        this.fh = fh;
    }

    public int getMh() {
    
    
        return mh;
    }

    public void setMh(int mh) {
    
    
        this.mh = mh;
    }
}

2. 案例所用代码&& 实现数据的回传:

在这里插入图片描述

HeightForecastActivity:

package com.example.myapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class LYXForecastHieghtActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    
    
Button btn_date,btn_Intent,btn_Bundle,btn_Serializable;
RadioGroup rg_gender;
RadioButton rb_man,rb_wom;
LinearLayout pagebg;
TextView atv_forecastMsg,btv_forecastMsg;
CheckBox checkBox,checkBox2,checkBox3,checkBox4;
EditText edt_birthday,edt_fatherheight,edt_motherheight;
int choiceSex;
int childheight;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hieght_forecast);
        //获取布局中对应的控件的id
        btn_date=findViewById(R.id.btn_date);
        btn_date.setOnClickListener(this);
        pagebg=findViewById(R.id.pagebg);
        edt_fatherheight=findViewById(R.id.edt_fatherHeight);
        edt_motherheight=findViewById(R.id.edt_motherheight);
        edt_birthday=findViewById(R.id.edt_birthday);
        atv_forecastMsg=findViewById(R.id.atv_forecastMsg);
        btv_forecastMsg=findViewById(R.id.btv_forecastMsg);

        btn_Intent=findViewById(R.id.btn_Intent);
        btn_Intent.setOnClickListener(this);
        btn_Bundle=findViewById(R.id.btn_Bundle);
        btn_Bundle.setOnClickListener(this);
        btn_Serializable=findViewById(R.id.btn_Serializable);
        btn_Serializable.setOnClickListener(this);


        rg_gender=findViewById(R.id.rg_gender);
        rb_man=findViewById(R.id.rb_man);
        rb_man.setOnCheckedChangeListener(this);
        rb_wom=findViewById(R.id.rb_wom);
        rb_wom.setOnCheckedChangeListener(this);

        checkBox=findViewById(R.id.checkBox);
        checkBox2=findViewById(R.id.checkBox2);
        checkBox3=findViewById(R.id.checkBox3);
        checkBox4=findViewById(R.id.checkBox4);

        //设置性别单选按钮 监听事件
        rg_gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    
    
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
    
    
                if(checkedId==rb_man.getId()){
    
    
                    choiceSex=1;    //选择男
                }
                else{
    
    
                    choiceSex=0;    //选择女
                }
            }
        });

    }

    //性别选择 点击弹框
    public void onCheckedChanged(CompoundButton compoundButton,boolean isCheck){
    
    
        switch(compoundButton.getId()){
    
    
            case R.id.rb_man:
                if (isCheck){
    
    
                    Toast.makeText(this, "男", Toast.LENGTH_SHORT).show();
                    pagebg.setBackgroundColor(Color.argb(100,228,237,193));
                }
                break;
            case R.id.rb_wom:
                if (isCheck){
    
    
                    Toast.makeText(this, "女", Toast.LENGTH_SHORT).show();
                    pagebg.setBackgroundColor(Color.argb(100,193,226,143));
                }
                break;
        }
    }

     // 按钮事件处理
    public void onClick(View view){
    
    
       switch(view.getId()){
    
    
           case R.id.btn_date:
               DatePickerDialog.OnDateSetListener birthdayListener=new DatePickerDialog.OnDateSetListener() {
    
    
                   @Override
                   public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    
    
                       month=month+1;
                       edt_birthday.setText(year+"-"+month+"-"+dayOfMonth);
                   }
               };
               Calendar calendar=Calendar.getInstance();
               int byear,bmonth,bday;
               byear=calendar.get(Calendar.YEAR)-20;
               bmonth=calendar.get(Calendar.MONTH);
               bday=calendar.get(Calendar.DAY_OF_MONTH);
               DatePickerDialog datePickerDialog=new DatePickerDialog(this,birthdayListener,byear,bmonth,bday);
               datePickerDialog.show();
               break;
           case R.id.btn_Intent:
               // 创建Intent ,明确源Activity和目标Activity
               Intent intent = new Intent(HeightForecastActivity.this,ResultIntentActivity.class);
               // 获取控件上的数据
               String str_birthday = edt_birthday.getText().toString();
               Boolean isMan = rb_man.isChecked();
               int fh = Integer.parseInt(edt_fatherheight.getText().toString());
               int mh = Integer.parseInt(edt_motherheight.getText().toString());
               Boolean ischeckBox = checkBox.isChecked();
               Boolean ischeckBox2 = checkBox2.isChecked();
               Boolean ischeckBox3 = checkBox3.isChecked();
               Boolean ischeckBox4 = checkBox4.isChecked();
               // 通过putExtra方法 在Intent上绑定数据
               //1. 第一种通过Intent直接传递数据:
               intent.putExtra("birthday",str_birthday);
               intent.putExtra("isMan",isMan);
               intent.putExtra("fh",fh);
               intent.putExtra("mh",mh);
               intent.putExtra("ischeckBox",ischeckBox);
               intent.putExtra("ischeckBox2",ischeckBox2);
               intent.putExtra("ischeckBox3",ischeckBox3);
               intent.putExtra("ischeckBox4",ischeckBox4);
               //最后通过startActivity方法实现Activity的跳转
               startActivity(intent);
               break;
           case R.id.btn_Bundle:
               // 创建Intent ,明确源Activity和目标Activity
               Intent intent2 = new Intent(HeightForecastActivity.this,ResultBundleActivity.class);
               // 获取控件上的数据
               String str_birthday2 = edt_birthday.getText().toString();
               Boolean isMan2 = rb_man.isChecked();
               int fh2 = Integer.parseInt(edt_fatherheight.getText().toString());
               int mh2 = Integer.parseInt(edt_motherheight.getText().toString());
               Boolean ischeckBox11 = checkBox.isChecked();
               Boolean ischeckBox22 = checkBox2.isChecked();
               Boolean ischeckBox23 = checkBox3.isChecked();
               Boolean ischeckBox24 = checkBox4.isChecked();

               //2. 第二种方法: Bundle传递数据,先通过putXXX方法把数据加载在Bundle中,然后再将bundle加载到intent 上
               Bundle bundle = new Bundle();
               bundle.putString("birthday",str_birthday2);
               bundle.putBoolean("isMan2",isMan2);
               bundle.putBoolean("ischeckBox11",ischeckBox11);
               bundle.putBoolean("ischeckBox22",ischeckBox22);
               bundle.putBoolean("ischeckBox23",ischeckBox23);
               bundle.putBoolean("ischeckBox24",ischeckBox24);
               bundle.putInt("fh2",fh2);
               bundle.putInt("mh2",mh2);
               intent2.putExtra("dataBundle",bundle);
               //最后通过startActivity方法实现Activity的跳转
               startActivity(intent2);
               break;
           case R.id.btn_Serializable:
               // 创建Intent ,明确源Activity和目标Activity
               Intent intent4 = new Intent(HeightForecastActivity.this,ResultSerializableActivity.class);
               // 获取控件上的数据
               String str_birthday4 = edt_birthday.getText().toString();
               Boolean isMan4 = rb_man.isChecked();
               int fh4 = Integer.parseInt(edt_fatherheight.getText().toString());
               int mh4 = Integer.parseInt(edt_motherheight.getText().toString());
               Boolean ischeckBox41 = checkBox2.isChecked();
               Boolean ischeckBox42 = checkBox3.isChecked();
               Boolean ischeckBox43 = checkBox4.isChecked();
               //第三种方法:传对象;
               FHInfo fhInfo=new FHInfo();
               fhInfo.setBirthday(str_birthday4);
               fhInfo.setMan(isMan4);
               fhInfo.setIscheckBox41(ischeckBox41);
               fhInfo.setIscheckBox42(ischeckBox42);
               fhInfo.setIscheckBox43(ischeckBox43);
               fhInfo.setFh(fh4);
               fhInfo.setMh(mh4);
               //只有序列化的对象才能通过intent进行传递
               intent4.putExtra("myInfo",fhInfo);
               //最后通过startActivity方法实现Activity的跳转
               startActivityForResult(intent4,1);
               break;
       }
    }


    //重写 onActivityResult() 获取并显示从ResultForecastActivity返回的结果
    protected  void onActivityResult(int requestCode,int resultCode,Intent data){
    
    
        super.onActivityResult(requestCode,resultCode,data);
        if (resultCode==1 && requestCode==1){
    
    

         // 获取从ResultForecastActivity返回的数据
        int childheight = data.getIntExtra("manheight",0);
        int childheight2 = data.getIntExtra("wonheight",0);
        String str_birthday4 = data.getStringExtra("str_birthday4");
        Boolean isMan4 = data.getBooleanExtra("isMan4",false);
        Boolean ischeckBox41 = data.getBooleanExtra("ischeckBox41",false);
        Boolean ischeckBox42 = data.getBooleanExtra("ischeckBox42",false);
        Boolean ischeckBox43 = data.getBooleanExtra("ischeckBox43",false);
        int fh4 = data.getIntExtra("fh4",0);
        int mh4 = data.getIntExtra("mh4",0);

        // 根据男女在HeightForecastActivity中的TextView显示不同的身高预测信息
            if (!isMan4.equals("")){
    
    
                atv_forecastMsg.setText("你的生日为:"+str_birthday4+"\n你是男生:"+isMan4+"\n你爱运动:"+ischeckBox41+"\n营养好 "+ischeckBox42+"\n爱喝牛奶"+ischeckBox43+"\n 你父亲的身高为:"+fh4+"\n 你母亲的身高为:"+mh4+"" + "\n你的身高预测为:"+childheight+"厘米");
            }else{
    
    
                atv_forecastMsg.setText("你的生日为:"+str_birthday4+"\n你是男生:"+isMan4+"\n你爱运动:"+ischeckBox41+"\n营养好 "+ischeckBox42+"\n爱喝牛奶"+ischeckBox43+"\n 你父亲的身高为:"+fh4+"\n 你母亲的身高为:"+mh4+"" + "\n你的身高预测为:"+childheight2+"厘米");
            }

        }
    }
}

ResultForecastActivity:

package com.example.myapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ResultLYXheightForecastActivity extends AppCompatActivity {
    
    
TextView btv_forecastMsg;
Button btn_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result_lyxheight_forecast);
        btn_result = findViewById(R.id.btn_result);
        btv_forecastMsg = findViewById(R.id.btv_forecastMsg);
        // 在目标Activity中通过 getIntent方法获取源Activity 传过来的Intent
        Intent intent = getIntent();

        // 第一种方式 直接通过Intent 进行数据传递
        // getXXXExtra 方法直接获取控件上相应的数据
       /* String str_birthday = intent.getStringExtra("birthday");
        Boolean isMan = intent.getBooleanExtra("isMan",false);
        Boolean ischeckBox = intent.getBooleanExtra("ischeckBox",false);
        Boolean ischeckBox2 = intent.getBooleanExtra("ischeckBox2",false);
        Boolean ischeckBox3 = intent.getBooleanExtra("ischeckBox3",false);
        Boolean ischeckBox4 = intent.getBooleanExtra("ischeckBox4",false);
        int fh = intent.getIntExtra("fh",0);
        int mh = intent.getIntExtra("mh",0);

        btv_forecastMsg = findViewById(R.id.btv_forecastMsg);
        btv_forecastMsg.setText("你的生日为:"+str_birthday+"\n你是男生:"+isMan+"\n你爱运动:"+ischeckBox+"\n营养好 "+ischeckBox2+"\n爱喝牛奶"+ischeckBox3+"\n爱睡觉 "+ischeckBox4+"\n 你父亲的身高为:"+fh+"\n 你母亲的身高为:"+mh);
*/
        //第二种方式:先从intent上接收Bundle数据,再从Bundle里面解析各个数据
        /*Bundle bundle=intent.getBundleExtra("dataBundle");
        String str_birthday2=bundle.getString("birthday");
        Boolean isMan2 = intent.getBooleanExtra("isMan2",false);
        Boolean ischeckBox11 = intent.getBooleanExtra("ischeckBox11",false);
        Boolean ischeckBox22 = intent.getBooleanExtra("ischeckBox22",false);
        Boolean ischeckBox23 = intent.getBooleanExtra("ischeckBox23",false);
        Boolean ischeckBox24 = intent.getBooleanExtra("ischeckBox24",false);
        int fh2=bundle.getInt("fh2",0);
        int mh2=bundle.getInt("mh2",0);
        btv_forecastMsg = findViewById(R.id.btv_forecastMsg);
        btv_forecastMsg.setText("你的生日为:"+str_birthday2+"\n你是男生:"+isMan2+"\n你爱运动:"+ischeckBox22+"\n营养好 "+ischeckBox11+"\n爱喝牛奶"+ischeckBox23+"\n爱睡觉 "+ischeckBox24+"\n 你父亲的身高为:"+fh2+"\n 你母亲的身高为:"+mh2);
*/

// 第三种 :传对象
        FHInfo myinfo= (FHInfo) intent.getSerializableExtra("myInfo");
        String str_birthday4=myinfo.getBirthday();
        Boolean isMan4=myinfo.isMan();
        Boolean ischeckBox41=myinfo.isIscheckBox41();
        Boolean ischeckBox42=myinfo.isIscheckBox42();
        Boolean ischeckBox43=myinfo.isIscheckBox43();
        int fh4=myinfo.getFh();
        int mh4=myinfo.getMh();
        btv_forecastMsg = findViewById(R.id.btv_forecastMsg);
        btv_forecastMsg.setText("你的生日为:"+str_birthday4+"\n你是男生:"+isMan4+"\n营养好 "+ischeckBox41+"\n爱喝牛奶"+ischeckBox42+"\n爱睡觉 "+ischeckBox43+"\n 你父亲的身高为:"+fh4+"\n 你母亲的身高为:"+mh4);


        btn_result.setOnClickListener(new View.OnClickListener() {
    
    

            @Override
            public void onClick(View v) {
    
    
                Intent intent = getIntent();
                FHInfo myinfo= (FHInfo) intent.getSerializableExtra("myInfo");
                String str_birthday4=myinfo.getBirthday();
                Boolean isMan4=myinfo.isMan();
                Boolean ischeckBox41=myinfo.isIscheckBox41();
                Boolean ischeckBox42=myinfo.isIscheckBox42();
                Boolean ischeckBox43=myinfo.isIscheckBox43();
                int fh4=myinfo.getFh();
                int mh4=myinfo.getMh();
                int  childheight = (int)((fh4*0.923+mh4)/2);
                int  childheight2 = (int)((fh4+mh4)*0.54);

                Intent intent4 = new Intent();
                intent4.putExtra("str_birthday4",str_birthday4);
                intent4.putExtra("isMan4",isMan4);
                intent4.putExtra("ischeckBox41",ischeckBox41);
                intent4.putExtra("ischeckBox42",ischeckBox42);
                intent4.putExtra("ischeckBox43",ischeckBox43);
                intent4.putExtra("fh4",fh4);
                intent4.putExtra("mh4",mh4);
                intent4.putExtra("manheight",childheight);
                intent4.putExtra("wonheight",childheight2);
                setResult(1,intent4);
                finish();
            }
     });


    }
}

xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:textSize="18dp"
        android:id="@+id/btv_forecastMsg"/>

    <Button
        android:id="@+id/btn_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:gravity="center|center_vertical"
        android:text="返回预测结果"/>

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/weixin_43853746/article/details/109537627
今日推荐