在为我的Android主题制作项目时遇到问题,我设法计算bmi然后将其显示给下一个活动但是在第一次尝试时,名称仅存在,并且在第二次尝试时计算的值显示
btnComp = (Button) findViewById(R.id.button3);
btnComp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = et.getText().toString();
String feet = et2.getText().toString();
String inch = et6.getText().toString();
String weyt = et3.getText().toString();
String age = et4.getText().toString();
String gender = et5.getText().toString();
String intento = res.getText().toString();
String meseg = msg2.getText().toString();
if ( ( et.getText().toString().trim().equals(""))) {
et.setError("Please enter your name");
et.requestFocus();
}
else if ( ( et2.getText().toString().trim().equals(""))) {
et2.setError("Please enter your Height!");
et2.requestFocus();
}
else if ( ( et6.getText().toString().trim().equals(""))) {
et6.setError("Please enter your Height!");
et6.requestFocus();
}
else if ( ( et3.getText().toString().trim().equals(""))) {
et3.setError("Please enter your Weight!");
et3.requestFocus();
}
else if ( ( et4.getText().toString().trim().equals(""))) {
et4.setError("Please enter your Age!");
et4.requestFocus();
}
else if ( ( et5.getText().toString().trim().equals(""))) {
et5.setError("Please enter your Gender!");
et5.requestFocus();
}
else{
double weight;
double inches;
double height;
double feet1;
double bmi;
String msg="";
feet1 = Double.parseDouble(et2.getText().toString());
inches = Double.parseDouble(et6.getText().toString());
weight = Double.parseDouble(et3.getText().toString());
feet1 = feet1 * 12;
inches = feet1 + inches;
bmi = (weight * 2.2 / (inches * inches))* 703;
if (bmi < 16) {
msg = "Severely Underweight";
} else if (bmi < 18.5) {
msg = "Underweight";
} else if (bmi < 25) {
msg = "Normal";
} else if (bmi < 30) {
msg = "Overweight";
} else {
msg = "Obese";
}
Intent intent = new Intent(newprofile.this,result.class);
res.setText(String.format("%.1f",bmi));
qwe.setText(String.format("%.2f",bmi));
view_msg.setText(msg);
msg2.setText(String.valueOf(msg));
intent.putExtra("user",name);
intent.putExtra("result",meseg);
intent.putExtra("height",feet);
intent.putExtra("inch",inch);
intent.putExtra("weight",weyt);
intent.putExtra("age",age);
intent.putExtra("gender",gender);
intent.putExtra("intenti",intento);
startActivity(intent);
}
这是第二个活动xml
TextView nameView= (TextView) findViewById(R.id.textView8);
TextView bmiView= (TextView) findViewById(R.id.insertbmi);
TextView bmibmi= (TextView) findViewById(R.id.resulta);
Intent intent = getIntent();
String bmishow = intent.getStringExtra("intenti");
bmiView.setText(bmishow);
String bmicheck = intent.getStringExtra("result");
bmibmi.setText(bmicheck);
String name = intent.getStringExtra("user");
nameView.setText(name);
对不起我的错误编码:&lt;
答案 0 :(得分:0)
从活动传递到活动的数据是正确的,但您的变量不正确。改变这一行:
intent.putExtra("result",meseg);
对此:
intent.putExtra("result",msg);
此外,您的代码看起来不错,但您可以使用更详细的变量名称:msg - &gt;信息。它没有伤害:))