ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验七 使用Intent在Activity间传输数据

2020-06-09 14:39:03  阅读:255  来源: 互联网

标签:String gender height 传输数据 Intent Activity import android public


实验七 使用Intent在Activity间传输数据

一、实验要求和目的

  1. 理解Activity组件的功能与作用;
  2. 掌握使用Intent在多个Activity组件间传输数据的方法;
  3. 掌握在AndroidManifest.xml中配置Activity组件的方法。
    二、实验环境
  4. 部署有Android Studio和Android SDK的主机;
  5. 建议在机房的HelloWorld例子上完成。
    三、上机操作参考步骤
    1、 完成一个体重计算器的应用程序开发。图1为该应用的首界面(即主Activity),用户可选择性别和输入身高值,点击“计算”按钮后启动图2所示的界面(即第二个Activity)。可以通过Intent携带性别、身高数据到第二个Activity,然后计算出体重并把三个数据显示到三个TextView中即可
    在这里插入图片描述
package com.example.shiyan7;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.example.shiyan7.Person;


public class Fatactivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fatactivity);
        TextView gender=findViewById(R.id.gender);
        TextView height=findViewById(R.id.height);
        TextView fat=findViewById(R.id.fat);
        Intent intent=getIntent();
        Person p=(Person)intent.getSerializableExtra("person");
        gender.setText("您的性别为"+p.getGender());
        height.setText("您的身高为"+p.getHeight());
        int fat1=Integer.parseInt(p.getHeight());
        if(p.getGender()=="男")
            fat.setText("您的标准体重为:"+(fat1-80)*0.7);
        else
            fat.setText("您的标准体重为:"+(fat1-70)*0.6);

    }
}

package com.example.shiyan7;
import java.io.Serializable;

public class Person implements Serializable
{
    private String height;
    private String gender;

    public Person(String height, String gender)
    {
        this.height= height;

        this.gender = gender;
    }

    public String getHeight()
    {
        return height;
    }

    public void setName(String height)
    {
        this.height= height;
    }

    public String getGender()
    {
        return gender;
    }

    public void setGender(String gender)
    {
        this.gender = gender;
    }
}
package com.example.shiyan7;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.wifi.aware.PeerHandle;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import com.example.shiyan7.Person;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bt1=findViewById(R.id.bt1);
        final RadioButton rb1=findViewById(R.id.rb1);
        final EditText height=findViewById(R.id.et1);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String gender = rb1.isChecked() ? "男" : "女";
                Person p = new Person(height.getText().toString(), gender);
                Bundle data = new Bundle();
                data.putSerializable("person", p);
                Intent intent = new Intent(MainActivity.this, Fatactivity.class);
                intent.putExtras(data);
                MainActivity.this.startActivity(intent);
            }
        });
    }
}

activity_fatactivity

<?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:id="@+id/gender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        >
    </TextView>
    <TextView
        android:id="@+id/height"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        >
    </TextView>
    <TextView
        android:id="@+id/fat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        >
    </TextView>
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv1"
        android:text="计算标准体重"
        android:textSize="30sp"
        android:layout_margin="5dp"
        ></TextView>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv2"
        android:textSize="18sp"
        android:text="性别"
        android:layout_below="@+id/tv1"
        android:layout_marginTop="15dp"
        ></TextView>
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rg1"
        android:layout_toRightOf="@+id/tv2"
        android:layout_below="@+id/tv1"
        android:orientation="horizontal"
        android:layout_marginTop="15dp"

        >
        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textSize="18sp"
            ></RadioButton>
        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="18sp"></RadioButton>
    </RadioGroup>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv3"
        android:textSize="18sp"
        android:text="身高"
        android:layout_below="@+id/tv2"
        android:layout_marginTop="30dp"
        ></TextView>
    <EditText
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:id="@+id/et1"
        android:layout_below="@id/tv2"
        android:layout_toRightOf="@id/tv3"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="20dp"
        ></EditText>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv4"
        android:textSize="18sp"
        android:text="CM"
        android:layout_below="@+id/tv2"
        android:layout_toRightOf="@id/et1"
        android:layout_marginTop="30dp"
        ></TextView>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt1"
        android:textSize="18sp"
        android:text="计算"
        android:layout_below="@+id/tv4"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="15dp"
        ></Button>
</RelativeLayout>

标签:String,gender,height,传输数据,Intent,Activity,import,android,public
来源: https://blog.csdn.net/wxwmb11/article/details/106605464

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有