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>

更多相关文章

  1. android 全屏函数
  2. Android(安卓)再按一次退出程序
  3. Android再按一次退出软件
  4. Android(安卓)点击回退按钮时弹出提示框
  5. Android(安卓)再按一次退出应用
  6. android 杀进程方法
  7. Android(安卓)关闭所有Activity完全退出程序方法
  8. Android中重写onBackPressed()方法实现双击退出
  9. BackHandler是全局的!!!

随机推荐

  1. 获取Android正在运行的任务和服务
  2. Android SystemUI状态栏添加图标
  3. Android小技巧&Android Studio快捷键(不定
  4. 说说在 Android 如何在全局获取 Context
  5. Android猜牌小游戏(改进版)
  6. Android设置背景图像重复【整理自网络】
  7. Android - 设定更改主Activity
  8. Android中 加载一张大图片Caused by: jav
  9. android 下拉列表(Spinner)的学习与应用
  10. tab上显示数字