Activity之间的数据传输的示例(实现根据身高计算标准体重)

本文介绍如何在Android应用中使用Java序列化技术来高效地传递复杂数据结构。通过创建实现了Serializable接口的Info类来存储用户的性别和身高信息,并在不同Activity间通过Bundle进行传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.编写一个实现java.io.Serializable接口的Java类,在该类中创建两个变量用于储存数据,并实现它的set和get方法

说明:

在Bundle传递数据包的时候,可以放入一个可序列化的对象。这样,当要传递的数据字段较多的时候,采用该方法比较方便。在例子中,为了在Bundle中放入一个可以序列化的对象,我们创建了一个可以序列化的Java类,这样方便储存序列化的对象。

2.可序列化的Info类

package activty.zhanghang.com.myapplication;

import java.io.Serializable;

/**
 * Created by dell-pc on 2016/2/8.
 */
public class Info implements Serializable {
    private static final long serialVersionUID = 1L;
    private String sex = "";//性别
    private int stature = 0;//身高
    public int getStature() {
        return stature;
    }

    public void setStature(int stature) {
        this.stature = stature;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

3.各个文件信息

MainActivty

package activty.zhanghang.com.myapplication;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
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.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btn;
    private  int RequestCode = 1;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.curculate);
        editText = (EditText) findViewById(R.id.edittext);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Info info = new Info();
                String heigth = editText.getText().toString();
                if("".equals(heigth)){
                    Toast.makeText(MainActivity.this,"请输入您的身高",Toast.LENGTH_SHORT).show();
                    return;
                }

                int stature = Integer.parseInt(editText.getText().toString());
                RadioGroup sex = (RadioGroup) findViewById(R.id.sex);
                //获取单选按钮的值
                for (int i=0;i<sex.getChildCount();i++){
                    RadioButton r = (RadioButton) sex.getChildAt(i);//根据索引值获取单选按钮
                    if(r.isChecked()){
                        info.setSex(r.getText().toString());
                        break;
                    }
                }
                info.setStature(stature);
                Bundle bundle = new Bundle();//实例化一个Bundle对象
                bundle.putSerializable("info", info);//将输入的基本信息保存在Bundle中
                Intent intent = new Intent(MainActivity.this,ResultActivity.class);
                intent.putExtras(bundle);
                startActivityForResult(intent,RequestCode);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==RequestCode){
            if (resultCode==ResultActivity.ResultCode){
                Bundle bundle = data.getExtras();
                String result = bundle.getString("result");
                Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
            }
        }
    }
}                                                                                                                         

对应的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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="activty.zhanghang.com.myapplication.MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请选择您的性别:"
            android:layout_gravity="center_vertical"/>
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sex"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入您的身高:"/>
    <EditText
        android:layout_width="227dp"
        android:layout_height="wrap_content"
        android:id="@+id/edittext" />
</LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算"
        android:id="@+id/curculate"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>


ResultActivity.java

package activty.zhanghang.com.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Created by dell-pc on 2016/2/8.
 */
public class ResultActivity extends AppCompatActivity {

    public static int ResultCode = 1;
    private Button btn;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        TextView sex = (TextView) findViewById(R.id.sex);
        TextView height = (TextView) findViewById(R.id.height);
        TextView normal = (TextView) findViewById(R.id.normal);
        btn = (Button) findViewById(R.id.back);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("result","谢谢您的使用");
                setResult(ResultCode, intent);
                finish();
            }
        });
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        Info info = (Info) bundle.getSerializable("info");//获取一个可以序列化的info对象
        sex.setText("您是一位"+info.getSex()+"士");//通过序列化的info对象获取序列化的值
        height.setText("您的身高是"+info.getStature()+"厘米");
        //显示计算后的数据
        normal.setText("您的标准体重是"+getWeight(info.getSex(),info.getStature())+"公斤");
    }
    /**
     * 计算
     */
    private String getWeight(String sex,float stature){
        String weight="";
        NumberFormat format = new DecimalFormat();
        if(sex.equals("男")){
            weight = format.format((stature-80)*0.7);
        }else{
            weight = format.format((stature-70)*0.6);
        }
        return weight;
    }
}


对应的xml布局文件
<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sex"
        android:textSize="30dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/height"
        android:textSize="30dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/normal"
        android:textSize="30dp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/back"
        android:text="返回"/>
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值