package com.BMI;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class BMIActivity extends Activity
{
private EditText et_height=null;
private EditText et_weight=null;
private Button bt_submit=null;
private TextView tv_result=null;
private Button bt_exit=null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_height=(EditText)findViewById(R.id.et_height);
et_weight=(EditText)findViewById(R.id.et_weight);
bt_submit=(Button)findViewById(R.id.bt_submit);
bt_exit=(Button)findViewById(R.id.bt_exit);
tv_result=(TextView)findViewById(R.id.tv_result);
bt_submit.setOnClickListener(listener);
bt_exit.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener()
{
public void onClick(View v)
{
Button button=(Button)v;
double height=0;
double weight=0;
switch(button.getId())
{
case R.id.bt_submit:
{
DecimalFormat nf=new DecimalFormat("0.00");
try
{
height=Double.parseDouble(et_height.getText().toString())/100;
} catch (Exception e)
{
height=1.65;
}
try
{
weight=Double.parseDouble(et_weight.getText().toString());
} catch (Exception e)
{
weight=60;
}
System.out.println(height);
System.out.println(weight);
double BMI = weight/(height*height);
if(BMI>0)
{
tv_result.setText("Your BMI is "+nf.format(BMI));
if(BMI < 20)
{
Toast.makeText(BMIActivity.this, "Too light",Toast.LENGTH_LONG).show();
}
else if(BMI >25)
{
Toast.makeText(BMIActivity.this, "Too Weight",Toast.LENGTH_LONG).show();
}
break;
}
else
{
Toast.makeText(BMIActivity.this,"Input Error",Toast.LENGTH_LONG).show();
break;
}
}
case R.id.bt_exit:
{
System.exit(0);
break;
}
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="身高(cm)"
android:textColor="#f00"
android:textStyle="bold"
android:textSize="20sp"
/>
<EditText
android:id="@+id/et_height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:hint="165"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="体重(kg)"
android:textColor="#f00"
android:textStyle="bold"
android:textSize="20sp"
/>
<EditText
android:id="@+id/et_weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:hint="60"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/bt_submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000"
android:textStyle="bold"
android:textSize="20sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="计算 BMI 值"
/>
<Button
android:id="@+id/bt_exit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000"
android:textStyle="bold"
android:textSize="20sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="退出"
/>
</LinearLayout>
<TextView
android:id="@+id/tv_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ff0"
android:textSize="20sp"
android:padding="20dp"
/>
</LinearLayout>