Tự học lập trình online

Lập trình online

Chương trình tính chỉ số béo phì BMI

BMI (Body Mass Index) chính là chỉ số cơ thể được các bác sĩ và các chuyên gia sức khỏe sử dụng để xác định tình trạng cơ thể của một người nào đó có bị béo phì, thừa cân hay quá gầy hay không. Thông thường, người ta dùng để tính toán mức độ béo phì .
Nhược điểm duy nhất của chỉ số BMI là nó không thể tính được lượng chất béo trong cơ thể - yếu tố tiềm ẩn các nguy cơ liên quan đến sức khỏe tương lai.
Người lớn và BMI

Chỉ số BMI của bạn được tính như sau: BMI = (trọng lượng cơ thể)/ (chiều cao x chiều cao).
- Trọng lượng cơ thể: tính bằng kg;
- Chiều cao x chiều cao: tính bằng m;
Bạn có thể tự đánh giá được chỉ số BMI của bản thân qua bảng thống kê dưới đây:
- Dưới chuẩn: BMI ít hơn 18.5 
- Chuẩn: BMI từ 18,5 - 25
- Thừa cân: BMI từ 25-30 
- Béo - nên giảm cân: BMI 30 - 40
- Rất béo – cần giảm cân ngay: BMI trên 40


Hôm nay mình sẽ giới thiệu chương trình tính chỉ số BMI 

Giao diện minh hoạ:

Đầu tiên ta thiết kế layout: activity_main.xml
Gồm:  2 EditText đề nhập dữ liệu vào; 2 Button để tính chỉ số BMI và nút nhập lại thông tin; tiếp theo là TextView hiển thị chỉ số BMI; sau đó là đánh giá, các đánh giá chỉ số tuân theo quy tắc ở trên. Bài này dựa trên ý tưởng đã có nhiều trên web.
file activity_main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="*"
    tools:context=".MainActivity">
    <TableRow android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span = "2"
            android:textColor="#FFFFFF"
            android:background="#0000A0"
            android:textSize="28sp"
            android:gravity="center"
            android:text="Chương trình tính chỉ số BMI"/>
    </TableRow>
    <TableRow android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="Chiều cao (cm)"/>
        <EditText
            android:id="@+id/edtChieuCao"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"/>
    </TableRow>
    <TableRow android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="Cân nặng (kg)"/>
        <EditText
            android:id="@+id/edtCanNang"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="numberSigned"
            android:ems="10"/>
    </TableRow>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button android:id="@+id/btnTinh"
            android:layout_width="150dp"
            android:layout_height="fill_parent"
            android:textSize="15sp"
            android:text="Tính chỉ số BMI"
            android:layout_gravity="top" />
        <Button android:id="@+id/btnReset"
            android:layout_width="200dp"
            android:layout_height="fill_parent"
            android:layout_span = "2"
            android:textSize="15sp"
            android:text="Nhập lại thông tin"/>
   </LinearLayout>
    <TableRow android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5sp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="Chỉ số BMI:"/>
        <TextView
            android:id="@+id/txtChiSo"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </TableRow>
    <TableRow android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5sp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="#FFBB00"
            android:text="Đánh giá: "/>
        <TextView
           android:id="@+id/txtDanhGia"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </TableRow>
    <Button android:id="@+id/btnThoat"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Thoát"/>
</TableLayout>

File MainActivity.java
package com.example.minhtung.tinhibm;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.Math;
import java.text.DecimalFormat;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    Button btnTinhBMI, btnReset, btnThoat;
    EditText editChieuCao, editCanNang;
    TextView txtChiSo, txtDanhGia;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getControlView();
        btnTinhBMI.setOnClickListener(this); //gọi nút Tính BMI
        btnReset.setOnClickListener(this); //gọi nút Nhập lại thông tin
        btnThoat.setOnClickListener(this); //Gọi nút thoát khỏi ứng dụng
    }
    @Override
    public void onClick(View v) {
        try {
            //Lấy giá trị nhập vào và ép kiểu về float
            double chieuCao = Double.parseDouble(editChieuCao.getText().toString());
            double canNang = Double.parseDouble(editCanNang.getText().toString());
            DecimalFormat df = new DecimalFormat("0.00"); //định dạng lấy đến 2 con số
            double BMI = canNang / Math.pow(chieuCao, 2) * 10000; //chiều cao * chiều cao
            switch (v.getId()) //lấy id của các Button
            {
                case R.id.btnTinh: //nếu id là Button Tính
                    txtChiSo.setText(df.format(BMI) + "");
                    if (BMI < 18)
                        txtDanhGia.setText("Bạn hơi gầy");
                    else if (18 <= BMI && BMI < 25)
                        txtDanhGia.setText("Bạn bình thường");
                    else if (25 <= BMI && BMI < 30)
                        txtDanhGia.setText("Bạn béo phì độ 1");
                    else if (30 <= BMI && BMI < 35)
                        txtDanhGia.setText("Bạn béo phì độ 2");
                    else if (35 <= BMI)
                        txtDanhGia.setText("Bạn béo phì độ 3");
                    break;
                case R.id.btnReset: //nếu id là Nhập lại thông tin
                    editChieuCao.setText("");
                    editCanNang.setText("");
                    txtChiSo.setText("");
                    txtDanhGia.setText("");
                    break;
                case R.id.btnThoat: //Nếu id là Thoát
                    finish();
                    System.exit(0);
            }
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), "Bạn chưa nhập dữ liệu", Toast.LENGTH_SHORT).show();
        }
    }
    private void getControlView() {
        btnTinhBMI = (Button) findViewById(R.id.btnTinh);
        editChieuCao = (EditText) findViewById(R.id.edtChieuCao);
        editCanNang = (EditText) findViewById(R.id.edtCanNang);
        txtChiSo = (TextView) findViewById(R.id.txtChiSo);
        txtDanhGia = (TextView) findViewById(R.id.txtDanhGia);
        btnReset = (Button) findViewById(R.id.btnReset);
        btnThoat = (Button) findViewById(R.id.btnThoat);
    }
}
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 nhận xét:

Đăng nhận xét