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

来源:互联网 发布:期货交易用什么软件 编辑:程序博客网 时间:2024/05/17 02:35

首先创建一个类用于保存性别和身高这个类继承接口 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;
}

0 0
原创粉丝点击