实现根据身高计算标准体重的难点

首先创建一个类用于保存性别和身高这个类继承接口 Serializable 

并添加这两个属性的getter setter方法


使用数据包 Bundle类传递数据包时,放入看序列化对象,传递数据字段比较多时,较为方便



if("".equals(((EditText)findViewById(R.id.stature)).getText().toString())){
Toast.makeText(BztzActivity.this, "请输入您的身高", Toast.LENGTH_LONG).show();
return;
}
int stature=Integer.parseInt(((EditText)findViewById(R.id.stature)).getText().toString());
RadioGroup sex=(RadioGroup) findViewById(R.id.sex);
for(int i=0;i<sex.getChildCount();i++){
r=(RadioButton) sex.getChildAt(i);
if(r.isChecked()){
info.setSex(r.getText().toString());
break;
}

}
info.setStature(stature);
Bundle bundle=new Bundle();
bundle.putSerializable("info", info);
Intent intent =new Intent(BztzActivity.this, ResultActivity.class);
intent.putExtras(bundle);
startActivity(intent);

获取Intent对象以及传递数据包




public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView sex=(TextView) findViewById(R.id.sex);
TextView stature=(TextView) findViewById(R.id.stature);
TextView weight=(TextView) findViewById(R.id.weight);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
Info info=(Info) bundle.getSerializable("info");
sex.setText("您是一位"+info.getSex()+"士");
stature.setText("您的身高是"+info.getStature()+"厘米");
weight.setText("您的体重是"+getWeight(info.getSex(), info.getStature())+"公斤");


}
private String getWeight(String sex,float stature){
String weight="";
NumberFormat format=new DecimalFormat();
if(sex.equals("男")){
weight=format.format((stature-80)*0.7);

}else{
weight=format.format((stature-70)*0.6);
}
return weight;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值