728x90
안드로이드 스튜디오로 BMI 계산기 만들기
코드
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/Edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="키(cm)" />
<EditText
android:id="@+id/Edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="몸무게(kg)" />
<Button
android:id="@+id/BtnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="확인" />
<TextView
android:id="@+id/TextResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#0000FF"
android:textSize="20dp" />
</LinearLayout>
MainActivity.java
package com.cookandroid.test;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText edit1, edit2;
private Button btn1;
private TextView textResult;
private String height, weight, BMI, result2;
private double result1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("비만도 계산기");
edit1 = findViewById(R.id.Edit1);
edit2 = findViewById(R.id.Edit2);
btn1 = findViewById(R.id.BtnAdd);
textResult = findViewById(R.id.TextResult);
btn1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
height = edit1.getText().toString();
weight = edit2.getText().toString();
비만도 계산 방법은 아래와 같다
신체질량지수(BMI) = 체중(kg) / [신장(m)]2
"Double.parseDouble(weight) / ((Double.parseDouble(height) / 100) * (Double.parseDouble(height) / 100));"
저체중< 20, 정상 20~24, 과체중 25~29, 비만>=30
result1 = Double.parseDouble(weight) / ((Double.parseDouble(height) / 100) * (Double.parseDouble(height) / 100));
if (result1 < 20) {
result2 = "저체중";
} else if (result1 <= 24 && result1 > 20) {
result2 = "정상";
} else if (result1 <= 30 && result1 > 24) {
result2 = "과체중";
} else {
result2 = "비만";
}
String BMI = String.format("%.2f", result1);
textResult.setText("귀하의 BMI = " + BMI + "이며, " + result2 + "입니다.");
return false;
}
});
}
}
결과
728x90
'개발 > Android' 카테고리의 다른 글
[Android Studio] 간단 그림판 만들기 (0) | 2021.11.17 |
---|---|
[Android Studio] Layout 종류 (0) | 2021.11.16 |
[Android Studio] 계산기 만들기 (2) | 2021.10.07 |