实验七 使用Intent在Activity间传输数据 一、实验要求和目的 activity_fatactivity activity_main.xml
二、实验环境
三、上机操作参考步骤
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); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://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>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://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>
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算