RadioButtonr=(RadioButton)sex.getChildAt(i);//根据索引值获取单选按钮
if(r.isChecked()){//判断单选按钮是否被选中
info.setSex(r.getText().toString());//获取被选中的单选按钮的值
break;//跳出for循环
}
}
info.setStature(stature);//设置身高
Bundlebundle=newBundle();//实例化一个Bundle对象
bundle.putSerializable("info",info);//将输入的基本信息保存到Bundle对象中
Intentintent=newIntent(MainActivity.this,ResultActivity.class);
intent.putExtras(bundle);//将bundle保存到Intent对象中
startActivity(intent);//启动intent对应的Activity
}
});
}
}
ResureActivity
packagecom.mingrisoft;
importjava.text.DecimalFormat;
importjava.text.NumberFormat;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.TextView;
publicclassResultActivityextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);//设置该Activity使用的布局
TextViewsex=(TextView)findViewById(R.id.sex);//获取显示性别的文本框
TextViewstature=(TextView)findViewById(R.id.stature);//获取显示身高的文本框
TextViewweight=(TextView)findViewById(R.id.weight);//获取显示标准体重的文本框
Intentintent=getIntent();//获取Intent对象
Bundlebundle=intent.getExtras();//获取传递的数据包
Infoinfo=(Info)bundle.getSerializable("info");//获取一个可序列化的info对象
sex.setText("您是一位"+info.getSex()+"士");//获取性别并显示到相应文本框中
stature.setText("您的身高是"+info.getStature()+"厘米");//获取身高并显示到相应文本框中
weight.setText("您的标准体重是"+getWeight(info.getSex(),info.getStature())+"公斤");//显示计算后的标准体重
Buttonbutton=(Button)findViewById(R.id.button1);
button.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
finish();
}
});
}
/**
*功能:
计算标准体重
*@paramsex
*@paramstature
*@return
*/
privateStringgetWeight(Stringsex,floatstature){
Stringweight="";//保存体重
NumberFormatformat=newDecimalFormat();
if(sex.equals("男")){//计算男士标准体重
weight=format.format((stature-80)*0.7);
}else{//计算女士标准体重
weight=format.format((stature-70)*0.6);
}
returnweight;
}
}
Info.java
packagecom.mingrisoft;
importjava.io.Serializable;
publicclassInfoimplementsSerializable{
privatestaticfinallongserialVersionUID=1L;
privateStringsex="";//?
?
privateintstature=0;//?
?
publicStringgetSex(){
returnsex;
}
publicvoidsetSex(Stringsex){
this.sex=sex;
}
publicintgetStature(){
returnstature;
}
publicvoidsetStature(intstature){
this.stature=stature;
}
}
AndroidManifest.xml
xmlversion="1.0"encoding="utf-8"?
>
android="
package="com.mingrisoft"
android:
versionCode="1"
android:
versionName="1.0">
minSdkVersion="15"/>
android:
icon="@drawable/c4"
android:
label="?
?
?
">
android:
label="?
?
?
?
"
android:
name=".MainActivity">
name="android.intent.action.MAIN"/>
name="android.intent.category.LAUNCHER"/>
android:
label="?
?
?
?
"
android:
icon="@drawable/ic_launcher"
android:
name=".ResultActivity">
三、实验结果
实验结果图
四、教师评价
指导教师签名: