Android:使用Intent在Activity间传输数据
实验:成一个体重计算器的应用程序开发。图1为该应用的首界面(即主Activity),用户可选择性别和输入身高值,点击“计算”按钮后启动图2所示的界面(即第二个Activity)。可以通过Intent携带性别、身高数据到第二个Activity,然后计算出体重并把三个数据显示到三个TextView中即可。
1、首先,创建两个布局文件Activity_Main.xml和result.xml分别用来显示输入界面和结果界面:
Activity_Main.xml界面布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算标准体重"
android:textSize="30dp"
/>
<TableRow>
<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="20dp"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/bt_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男性"
android:textSize="16dp"
android:checked="true"
/>
<RadioButton
android:id="@+id/bt_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女性"
android:textSize="16dp"
/>
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="身高:"
android:textSize="20dp"
/>
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
/>
<TextView
android:id="@+id/tv_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CM"
android:textSize="20dp"
/>
</TableRow>
<Button
android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算"
android:textSize="20dp"
/>
</TableLayout>
布局预览:
结果界面布局result.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
/>
<TextView
android:id="@+id/tv_height"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
/>
<TextView
android:id="@+id/tv_weight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
/>
</LinearLayout>
布局预览:
2、其次,实现功能逻辑:
Activity_Main.java代码如下:
package com.example.test_7;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bn = findViewById(R.id.bn);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText height = findViewById(R.id.et);
RadioButton male = findViewById(R.id.bt_1);
String gender = male.isChecked()? "男":"女";
int weight = 0;
int w = Integer.parseInt(height.getText().toString());
if(gender=="男"){
weight = (int)( (w - 80)*0.7);
}else {
weight = (int)((w - 70)*0.6);
}
Person p =new Person(height.getText().toString(),gender,weight);
Bundle data = new Bundle();
data.putSerializable("person",p);
Intent intent = new Intent(MainActivity.this,result.class);
intent.putExtras(data);
startActivity(intent);
}
});
}
}
result.java代码如下:
package com.example.test_7;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class result extends Activity {
public void onCreate (Bundle savedInstanceStata)
{
super.onCreate(savedInstanceStata);
setContentView(R.layout.result);
TextView gender = findViewById(R.id.tv_gender);
TextView height = findViewById(R.id.tv_height);
TextView weight = findViewById(R.id.tv_weight);
Intent intent = getIntent();
Person p = (Person) intent.getSerializableExtra("person");
gender.setText("您的性别是:"+ p.getGender());
height.setText("您的身高是:"+ p.getHeight()+"CM");
weight.setText("您的标准体重是:"+ p.getWeight()+"kg");
}
}
3、在编写上述代码时,根据用户输入创建了一个Person对象,Person类在这里只是自己创建的一个简单的DTO对象,并且该Person类实现了java.io.Serializable接口,因此,Person对象是可序列化的。(注:你或许会问什么是DTO对象?其实,就是,数据传输对象,也就是用来获取你所创建的类对象的值(提示一下:需要用到getter和setter哦!)就是下边这段代码。)
package com.example.test_7;
import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID=1l;
private String height;
private String gender;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
private int weight;
public Person()
{
}
/*
* @param name
* @param pass
* @param gender
*/
public Person(String height,String gender,int weight)
{
this.height=height;
this.gender=gender;
this.weight=weight;
}
public String getHeight()
{
return this.height;
}
public void setHeight(String height)
{
this.height=height;
}
public String getGender()
{
return this.gender;
}
public void setGender(String gender)
{
this.gender=gender;
}
}
最终实验截图如下: