• 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>

  • 相关阅读:
    map 取最大value
    tmux 常用快捷键
    史蒂夫 乔布斯(Steve Jobs)在 Stanford 2005年毕业典礼上的演讲(中英文对照)
    黄聪:WordPress后台添加侧边栏菜单(WP教程add_menu_page)
    黄聪:ASP.NET AJAX入门系列(1) AjaxControlToolkit安装篇
    黄聪:ASP.NET AJAX入门系列(2) Accordion控件
    黄聪:wordpress调用函数大全
    黄聪:使用Wordpress中的wpdb类操作数据库
    黄聪:C#的Microsoft图表控件
    黄聪:如何写出兼容性强的CSS代码,适应各种浏览器:IE、火狐等
  • 原文地址:https://www.cnblogs.com/Neddy/p/2170330.html
Copyright © 2020-2023  润新知