目标:开发一款BMI体质指数计算器。用户在主界面中输入身高和体重,单击“计算BMI值”按钮后,在界面二通过TextView显示相应的结论;界面二点击返回能够回到主界面。
体质指数与胖瘦程度表
胖瘦程度 | 体质指数 |
---|---|
过轻 | 男性低于20,女性低于19 |
适中 | 男性20—25,女性19—24 |
超重 | 男性25—30,女性24—29 |
肥胖 | 男性20—25,女性19—24 |
严重肥胖 | 男性高于35,女性高于34 |
BMI是体质指数,一种公认的评定个人体质胖瘦程度的分级方法,具体的计算方法如下:
体质指数(BMI)=体重(kg)/身高(m)^2
实现代码如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="@drawable/img04"tools:context="com.example.jinjin.bimtest.MainActivity"android:weightSum="1"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="身高(cm)" /><EditTextandroid:id="@+id/height"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="体重(kg)" /><EditTextandroid:id="@+id/weight"android:layout_width="match_parent"android:layout_height="wrap_content" /><RadioGroupandroid:id="@+id/radioGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/btnMan"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"android:checked="true"/><RadioButtonandroid:id="@+id/btnWoman"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"/></RadioGroup><Buttonandroid:id="@+id/btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="150dp"android:layout_marginTop="100dp"android:background="#E6E6FA"android:text="计算BMI值" />
</LinearLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:background="@drawable/img04"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="#5F9EA0"android:textSize="20dp"android:layout_marginLeft="90dp"android:layout_marginTop="80dp"android:text="文本" /><Buttonandroid:id="@+id/btnback"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="155dp"android:layout_marginTop="135dp"android:background="#E6E6FA"android:text="返回"/>
</LinearLayout>
MainActivity.java
package com.example.jinjin.bimtest;import android.content.Intent;
import android.icu.text.DecimalFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;public class MainActivity extends AppCompatActivity {Button button;EditText height;EditText weight;RadioButton boy;RadioButton girl;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);height = (EditText) findViewById(R.id.height);weight = (EditText) findViewById(R.id.weight);boy = (RadioButton) findViewById(R.id.btnMan);girl = (RadioButton) findViewById(R.id.btnWoman);button = (Button) findViewById(R.id.btn);button.setOnClickListener(new mclick());}class mclick implements View.OnClickListener {@RequiresApi(api = Build.VERSION_CODES.N)@Overridepublic void onClick(View view) {String h = height.getText().toString();String w = weight.getText().toString();String msg = "";String bmi = "";double res = 0, heightNum = 0, weightNum = 0;if (!h.isEmpty() && !w.isEmpty()) {heightNum = Double.parseDouble(h); // 强制类型转换,把括号里的类型变成doubleweightNum = Double.parseDouble(w);res = 10000 * weightNum / (heightNum * heightNum);DecimalFormat df = new DecimalFormat("#.00");bmi = df.format(res);System.out.println(bmi);}Log.i("bmi",bmi);msg="你的BMI值是".toString();if (boy.isChecked()) {if (res>35){msg+=bmi+"\n"+"你的体型过于肥胖!要减肥了!".toString();}else if (res>30){msg+=bmi+"\n"+"你的体型是肥胖!要减肥了!".toString();}else if (res>25){msg+=bmi+"\n"+"你的体型是过重!可以减减肥了!".toString();}else if (res>20){msg+=bmi+"\n"+"你的体型十分匀称呀!".toString();}else{msg+=bmi+"\n"+"你的体型是过轻!要多吃点!".toString();}}if (girl.isChecked()) {if (res>34){msg+=bmi+"\n"+"你的体型过于肥胖!要减肥了!".toString();}else if (res>29){msg+=bmi+"\n"+"你的体型是肥胖!要减肥了!".toString();}else if (res>24){msg+=bmi+"\n"+"你的体型是过重!可以减减肥了!".toString();}else if (res>19){msg+=bmi+"\n"+"你的体型十分匀称呀!".toString();}else{msg+=bmi+"\n"+"你的体型是过轻!要多吃点!".toString();}}System.out.println(msg);Intent intent=new Intent(MainActivity.this,SecondActivity.class);Bundle bundle=new Bundle();bundle.putString("msg",msg);intent.putExtras(bundle);startActivity(intent);}}
}
SecondActivity.java
package com.example.jinjin.bimtest;import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/*** Created by jinjin on 2020/5/17.*/public class SecondActivity extends AppCompatActivity {TextView textView;Button btnback;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);textView = (TextView) findViewById(R.id.textView);Bundle bundle = this.getIntent().getExtras();String msg = bundle.getString("msg");textView.setText(msg);btnback = (Button) findViewById(R.id.btnback);btnback.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(SecondActivity.this, MainActivity.class);startActivity(intent);}});}
}