<?xml version="1.0" encoding="utf-8"?>
<!--添加布局属性-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? xmlns:app="http://schemas.android.com/apk/res-auto"
??? xmlns:tools="http://schemas.android.com/tools"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? android:orientation="vertical"
??? android:background="@drawable/lm"
??? tools:context=".MainActivity">
<!--??? 文本框组件-->
??? <TextView
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="身高(cm):"
??????? android:textSize="20dp"
??????? android:textColor="#000"
?????? />
??? <EditText
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:hint="请输入身高"
??????? android:id="@+id/b1"
??????? android:maxLines="1"
??????? android:textSize="20dp"
??????? android:textColor="#000"/>
??? <TextView
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="体重(kg):"
??????? android:textSize="20dp"
??????? android:textColor="#000"
??????? />
??? <EditText
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:hint="请输入体重"
??????? android:id="@+id/b2"
??????? android:maxLines="1"
??????? android:textSize="20dp"
??????? android:textColor="#000"/>
??? <Button
??????? android:id="@+id/s"
??????? android:layout_width="165dp"
??????? android:layout_height="wrap_content"
??????? android:layout_gravity="center"
??????? android:background="@drawable/shape"
??????? android:text="计算体质指数"
??????? android:textSize="25dp" />
?? ?<LinearLayout
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:orientation="horizontal">
??????? <TextView
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:text="您的BMI值为:"
??????????? android:textColor="#000"
??????????? android:textSize="20dp" />
??????? <TextView
??????????? android:id="@+id/BMI"
??????????? android:layout_width="274dp"
??????????? android:layout_height="wrap_content"
??????????? android:ellipsize="end"
??????????? android:textColor="#000"
??????????? android:textSize="30dp" />
??? </LinearLayout>
??? <LinearLayout
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:orientation="horizontal">
??? <TextView
??????? android:layout_width="wrap_content"
??????? android:layout_height="match_parent"
??????? android:text="建议:"
??????? android:textColor="#000"
??????? android:textSize="20dp" />
??? <TextView
??????? android:id="@+id/p"
??????? android:layout_width="348dp"
??????? android:layout_height="93dp"
??????? android:layout_gravity="center"
??????? android:textColor="#000"
??????? android:textSize="30dp" />
??? </LinearLayout>
??? <RelativeLayout
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent"
??????? >
??????? <Button
??????????? android:layout_width="110dp"
??????????? android:layout_height="wrap_content"
??????????? android:background="@drawable/shape"
??????????? android:layout_marginTop="120dp"
??????????? android:id="@+id/g"
??????????? android:text="关于BMI"
??????????? android:textSize="25dp" />
??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="wrap_content"
??????????? android:background="@drawable/shape"
??????????? android:id="@+id/t"
??????????? android:layout_marginTop="120dp"
??????????? android:layout_marginLeft="322dp"
??????????? android:text="退出"
??????????? android:textSize="25dp" />
??? </RelativeLayout>
</LinearLayout>
</LinearLayout>
package com.example.iii;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.service.voice.VoiceInteractionSession;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
??? @Override
??? protected void onCreate(Bundle savedInstanceState) {
??????? Button a,g,t;
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.activity_main);
??????? a=(Button) findViewById(R.id.s);
??????? a.setOnClickListener(new View.OnClickListener() {
??????????? @Override
??????????? public void onClick(View view) {
??????????????? EditText b1=(EditText) findViewById(R.id.b1);
??????????????? EditText b2=(EditText) findViewById(R.id.b2);
??????????????? TextView BMI=(TextView) findViewById(R.id.BMI);
??????????????? TextView p=(TextView) findViewById(R.id.p);
??????????????? Double m=Double.parseDouble(b1.getText().toString());
??????????????? Double n=Double.parseDouble(b2.getText().toString());
??????????????? Double b=n/(m*m)*10000;
??????????????? if (b<18.5){
?????????????????????? BMI.setText(b.toString());
?????????????????????? p.setText("过轻,建议提高营养摄入!");
??????????????? }else if (b<24){
??????????????????? BMI.setText(b.toString());
??????????????????? p.setText("适中,建议保持当前状态!");
??????????????? }else if (b<27){
??????????????????? BMI.setText(b.toString());
??????????????????? p.setText("过重,建议注意饮食适量!");
??????????????? }else if (b<32){
? ??????????????????BMI.setText(b.toString());
??????????????????? p.setText("肥胖,建议多运动!");
??????????????? }else if (b>=32){
??????????????????? BMI.setText(b.toString());
??????????????????? p.setText("非常肥胖,建议就医!");
??????????????? }
??????????? }
??????? });
??????? t=(Button)findViewById(R.id.t);
??????? t.setOnClickListener(new View.OnClickListener() {
??????????? @Override
??????????? public void onClick(View view) {
??????????????? MainActivity.this.finish();
??????????? }
??????? });
??????? g=(Button)findViewById(R.id.g);
??????? g.setOnClickListener(new View.OnClickListener() {
??????????? @Override
??????????? public void onClick(View view) {
??????????????? Intent intent=new Intent(MainActivity.this, Main2Activity.class);
???????? ???????startActivity(intent);
??????????? }
??????? });
??? }