آموزش ساخت محاسبه گر شاخص توده بدنی BMI

در این آموزش می خواهیم نحوه ایجاد یک ماشین حساب شاخص توده بدنی (BMI) آموزش ببینیم.

این آموزش مناسب مبتدیانی است که می خواهند نحوه ایجاد این ماشین حساب را بررسی کنند.

شاخص توده بدنی یا (BMI (Body Mass Index مقداری است که از وزن و قد یک فرد حاصل می شود.

شاخص توده بدنی= kg/m^2

قدم اول:تعریف چیدمان

قدم اول تعریف یک چیدمان مناسب است که به کاربر امکان می دهد مقادیر قد و وزن را وارد کند تا شاخص توده بدنی محاسبه شود.

علاوه بر این ما به یک دکمه برای شروع محاسبه شاخص توده بدنی و همچنین یک TextView برای نمایش نتیجه نیاز خواهیم داشت.

پس در کل به موارد زیر نیاز داریم:

  1. دو EditText
  2. یک Button
  3. یک TextView

چیدمان اپلیکیشن ما به صورت زیر خواهد بود:

<?xml version=&quot1.0&quot encoding=&quotutf-8&quot?>
<LinearLayout xmlns:android=&quothttp://schemas.android.com/apk/res/android&quot
    xmlns:tools=&quothttp://schemas.android.com/tools&quot
    android:id=&quot@+id/activity_main&quot
    android:layout_width=&quotmatch_parent&quot
    android:layout_height=&quotmatch_parent&quot
    android:paddingBottom=&quot@dimen/activity_vertical_margin&quot
    android:paddingLeft=&quot@dimen/activity_horizontal_margin&quot
    android:paddingRight=&quot@dimen/activity_horizontal_margin&quot
    android:paddingTop=&quot@dimen/activity_vertical_margin&quot
    android:orientation=&quotvertical&quot
    tools:context=&quotcom.ssaurel.bmicalculator.MainActivity&quot>

    <TextView
        android:text=&quot@string/weight&quot
        android:layout_width=&quotwrap_content&quot
        android:layout_height=&quotwrap_content&quot
        android:layout_gravity=&quotcenter_horizontal&quot
        android:gravity=&quotcenter_horizontal&quot
        android:layout_marginTop=&quot50dp&quot
        android:textSize=&quot20sp&quot/>

    <EditText
        android:id=&quot@+id/weight&quot
        android:layout_width=&quotwrap_content&quot
        android:layout_height=&quotwrap_content&quot
        android:layout_gravity=&quotcenter_horizontal&quot
        android:layout_marginTop=&quot10dp&quot
        android:ems=&quot6&quot
        android:inputType=&quotnumber|numberDecimal&quot
        android:textSize=&quot20sp&quot/>

    <TextView
        android:text=&quot@string/height&quot
        android:layout_width=&quotwrap_content&quot
        android:layout_height=&quotwrap_content&quot
        android:layout_gravity=&quotcenter_horizontal&quot
        android:gravity=&quotcenter_horizontal&quot
        android:layout_marginTop=&quot50dp&quot
        android:textSize=&quot20sp&quot/>

    <EditText
        android:id=&quot@+id/height&quot
        android:layout_width=&quotwrap_content&quot
        android:layout_height=&quotwrap_content&quot
        android:layout_gravity=&quotcenter_horizontal&quot
        android:layout_marginTop=&quot10dp&quot
        android:ems=&quot6&quot
        android:inputType=&quotnumber|numberDecimal&quot
        android:textSize=&quot20sp&quot/>

    <Button
        android:id=&quot@+id/calc&quot
        android:layout_width=&quotwrap_content&quot
        android:layout_height=&quotwrap_content&quot
        android:layout_gravity=&quotcenter_horizontal&quot
        android:layout_marginTop=&quot25dp&quot
        android:=&quotcalculateBMI&quot
        android:text=&quot@string/calculateBMI&quot
        />

    <TextView
        android:id=&quot@+id/result&quot
        android:layout_width=&quotwrap_content&quot
        android:layout_height=&quotwrap_content&quot
        android:layout_gravity=&quotcenter_horizontal&quot
        android:gravity=&quotcenter_horizontal&quot
        android:layout_marginTop=&quot25dp&quot
        android:textSize=&quot20sp&quot/>

</LinearLayout>

قدم دوم:نوشتن کد Java

اکنون می توانیم کد جاوا را در Main Activity تایپ کنیم.

کد جاوا اپلیکیشن ما به صورت زیر خواهد بود:

package com.ssaurel.bmicalculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText height;
    private EditText weight;
    private TextView result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        height = (EditText) findViewById(R.id.height);
        weight = (EditText) findViewById(R.id.weight);
        result = (TextView) findViewById(R.id.result);
    }

    public void calculateBMI(View v) {
        String heightStr = height.getText().toString();
        String weightStr = weight.getText().toString();

        if (heightStr != null && !&quot&quot.equals(heightStr)
                && weightStr != null  &&  !&quot&quot.equals(weightStr)) {
            float heightValue = Float.parseFloat(heightStr) / 100;
            float weightValue = Float.parseFloat(weightStr);

            float bmi = weightValue / (heightValue * heightValue);

            displayBMI(bmi);
        }
    }

    private void displayBMI(float bmi) {
        String bmiLabel = &quot&quot

        if (Float.compare(bmi, 15f) <= 0) {
            bmiLabel = getString(R.string.very_severely_underweight);
        } else if (Float.compare(bmi, 15f) > 0  &&  Float.compare(bmi, 16f) <= 0) {
            bmiLabel = getString(R.string.severely_underweight);
        } else if (Float.compare(bmi, 16f) > 0  &&  Float.compare(bmi, 18.5f) <= 0) {
            bmiLabel = getString(R.string.underweight);
        } else if (Float.compare(bmi, 18.5f) > 0  &&  Float.compare(bmi, 25f) <= 0) {
            bmiLabel = getString(R.string.normal);
        } else if (Float.compare(bmi, 25f) > 0  &&  Float.compare(bmi, 30f) <= 0) {
            bmiLabel = getString(R.string.overweight);
        } else if (Float.compare(bmi, 30f) > 0  &&  Float.compare(bmi, 35f) <= 0) {
            bmiLabel = getString(R.string.obese_class_i);
        } else if (Float.compare(bmi, 35f) > 0  &&  Float.compare(bmi, 40f) <= 0) {
            bmiLabel = getString(R.string.obese_class_ii);
        } else {
            bmiLabel = getString(R.string.obese_class_iii);
        }

        bmiLabel = bmi + &quot\n\n&quot + bmiLabel;
        result.setText(bmiLabel);
    }
}

ما مقادیر وارد شده توسط کاربر را برای وزن و قد دریافت می کنیم. قد به سانتی متر وارد می شود. برای فرمول باید قد را به متر داشته باشیم. بنابراین ، مقدار وارد شده را به 100 تقسیم می کنیم. سپس اعداد را برای محاسبه BMI اعمال می کنیم:

float bmi = weightValue / (heightValue * heightValue);

برای تشخیص نتایج از جدول زیر استفاده میکنیم:

بعد از آن فقط مقدار BMI همراه با نتیجه در TextView نمایش داده خواهد شد:

Writer : MD_CODER

منبع: سایت محمد دادخواه