zoukankan      html  css  js  c++  java
  • Android BMI程式

    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>

  • 相关阅读:
    hcharts实现堆叠柱形图
    程序员常用的六大技术博客类
    程序员常用的六大技术博客类
    程序员常用的六大技术博客类
    程序员常用的六大技术博客类
    前端几个常用简单的开发手册拿走不谢
    web开发快速提高工作效率的一些资源
    web开发快速提高工作效率的一些资源
    基础知识(5)- 继承
    51 NOD 1384 全排列(STL 搜索)
  • 原文地址:https://www.cnblogs.com/Neddy/p/2170330.html
Copyright © 2011-2022 走看看