我的当前源代码存在问题。我对此很新。这是关于BMI计算,并将使用alertdialog显示输出。我已经把条件放了,但输出总是“你是超重”的其他条件,即使我的输入是合法的应该是IDEAL输出。
活动在跑步中没有出错,但我觉得这是我不知道的计算问题。如果这是一个重复的问题,我真的很抱歉。
public class BMIcalcu extends Activity{
EditText weight, height;
final Context context = this;
Button calculate;
float result, height1, weight1;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_calcu);
height = (EditText)findViewById(R.id.editText1);
weight = (EditText)findViewById(R.id.editText2);
calculate = (Button)findViewbyId(R.id.btn_bmi);
String str1 = height.getText().toString();
String str2 = weight.getText().toString();
try{
height1 = Float.parseFloat(str1)/100;
weight1 = Float.parseFloat(str2);
}catch(NumberFormatException nfe){
}
calculate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
result = weight1 / (height1*height1);
if(result<18.5){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Your BMI");
alertDialogBuilder
.setMessage("You are UNDERWEIGHT! START EATING!")
.setCancelable(false)
.setPositiveButton("List", new DialogInterface.OnClickLister(){
public void onClick(DialogInterface dialog, int id){
Intent intent = new Intent(BMIcalcu.this, list.class);
startActivity(intent);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
else if(result<25){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Your BMI");
alertDialogBuilder
.setMessage("You are on your IDEAL!")
.setCancelable(false)
.setPositiveButton("List", new DialogInterface.OnClickLister(){
public void onClick(DialogInterface dialog, int id){
Intent intent = new Intent(BMIcalcu.this, list.class);
startActivity(intent);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
else{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Your BMI");
alertDialogBuilder
.setMessage("You are OVERWEIGHT! START EXERCISING!")
.setCancelable(false)
.setPositiveButton("List", new DialogInterface.OnClickLister(){
public void onClick(DialogInterface dialog, int id){
Intent intent = new Intent(BMIcalcu.this, list.class);
startActivity(intent);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
}
}
这是xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_gravity="center"
android:layout_width="266dp"
android:layout_height="wrap_content"
android:inputType="text|number"
android:hint="Height (Centimeter)"
android:ems="10"
android:id="@+id/editText1" />
<EditText
android:layout_gravity="center"
android:layout_width="266dp"
android:layout_height="wrap_content"
android:inputType="text|number"
android:hint="Weight (Kilogram)"
android:ems="10"
android:id="@+id/editText2"/>
<Button
android:id="@+id/btn_bmi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_below="@+id/editText2"
android:text="Calculate"/>
</LinearLayout>
答案 0 :(得分:0)
在onClick()中移动高度和重量分配。
calculate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String str1 = height.getText().toString();
String str2 = weight.getText().toString();
try{
height1 = Float.parseFloat(str1)/100;
weight1 = Float.parseFloat(str2);
}catch(NumberFormatException nfe){
}
result = weight1 / (height1*height1);
String msg = "";
if(result<18.5){
msg = "You are UNDERWEIGHT! START EATING!";
}
else if(result<25){
msg = "You are on your IDEAL!";
}
else{
msg = "You are UNDERWEIGHT! START EATING!";
}
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Your BMI");
alertDialogBuilder
.setMessage(msg)
.setCancelable(false)
.setPositiveButton("List", new DialogInterface.OnClickLister(){
public void onClick(DialogInterface dialog, int id){
Intent intent = new Intent(BMIcalcu.this, list.class);
startActivity(intent);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
答案 1 :(得分:0)
您目前正在weight
内设置height
和onCreate()
的值。首次创建onCreate
时会调用Activity
。 onCreate
仅在Activity生命周期中调用一次。有关活动的更多信息,请访问:https://developer.android.com/guide/components/activities.html。
要回答您的问题,除非您要设置默认值,否则不应在weight
中设置height
和onCreate
的值。尝试将它们移到onClick
方法中:
calculate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String str1 = height.getText().toString();
String str2 = weight.getText().toString();
try {
height1 = Float.parseFloat(str1)/100;
weight1 = Float.parseFloat(str2);
result = weight1 / (height1*height1);
}
catch(NumberFormatException nfe) {
}
// Continue Alert Dialog stuff down here...
你为什么遇到这个问题?
目前,您的Activity
启动并且onCreate
被调用。您正在创建对xml的引用,然后尝试设置weight
和height
的值。但是,当调用onCreate
时,用户尚未在EditText
weight
height
中输入任何内容。
因此,在此之后,假设用户输入了值。这些值不会记录在任何地方。
然后,当用户单击Button
以查看其BMI时,您尝试计算结果。结果中没有任何内容,因此始终会调用else
。这就是你继续看到你输出的原因。