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

实验七 使用Intent在Activity间传输数据,第1张

概述实验七使用Intent在Activity间传输数据一、实验要求和目的理解Activity组件的功能与作用;掌握使用Intent在多个Activity组件间传输数据的方法;掌握在AndroidManifest.xml中配置Activity组件的方法。二、实验环境部署有AndroidStudio和AndroidSDK的主机;建议在机房的

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

一、实验要求和目的

理解Activity组件的功能与作用;掌握使用Intent在多个Activity组件间传输数据的方法;掌握在AndroIDManifest.xml中配置Activity组件的方法。
二、实验环境部署有AndroID Studio和AndroID SDK的主机;建议在机房的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 set@R_419_6889@(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>
总结

以上是内存溢出为你收集整理的实验七 使用Intent在Activity间传输数据全部内容,希望文章能够帮你解决实验七 使用Intent在Activity间传输数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1058165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存