一、 界面布局:
(1)、 主界面:
Activity_main.xml源代码:
<?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" ????tools:context=".MainActivity"> ????//用户 ????<LinearLayout ????????android:layout_width="wrap_content" ????????android:layout_height="wrap_content" ????????android:orientation="horizontal" ????????android:layout_marginLeft="50dp" ????????android:layout_marginTop="50dp"> ????????<TextView ????????????android:layout_width="wrap_content" ????????????android:layout_height="wrap_content" ????????????android:text="用户" ????????????android:textSize="40dp"/> ????????<EditText ????????????android:id="@+id/ID" ????????????android:layout_width="200dp" ????????????android:layout_height="match_parent" ????????????/> ????????></LinearLayout> ????//密码 ????<LinearLayout ????????android:layout_width="wrap_content" ????????android:layout_height="wrap_content" ????????android:orientation="horizontal" ????????android:layout_marginLeft="50dp" ????????android:layout_marginTop="50dp"> ????????<TextView ????????????android:layout_width="wrap_content" ????????????android:layout_height="wrap_content" ????????????android:text="密码" ????????????android:textSize="40dp"/> ????????<EditText ????????????android:id="@+id/PASS" ????????????android:layout_width="200dp" ????????????android:layout_height="match_parent" ????????????android:inputType="numberPassword" ????????????/> ????????></LinearLayout> ????//身高 ????<LinearLayout ????????android:layout_width="wrap_content" ????????android:layout_height="wrap_content" ????????android:orientation="horizontal" ????????android:layout_marginLeft="50dp" ????????android:layout_marginTop="50dp"> ????????<TextView ????????????android:layout_width="wrap_content" ????????????android:layout_height="wrap_content" ????????????android:text="身高" ????????????android:textSize="40dp"/> ????????<EditText ????????????android:id="@+id/H" ????????????android:text="m" ????????????android:layout_width="200dp" ????????????android:layout_height="match_parent" ????????????/> ????????></LinearLayout> ????//体重 ????<LinearLayout ????????android:layout_width="wrap_content" ????????android:layout_height="wrap_content" ????????android:orientation="horizontal" ????????android:layout_marginLeft="50dp" ????????android:layout_marginTop="50dp"> ????????<TextView ????????????android:layout_width="wrap_content" ????????????android:layout_height="wrap_content" ????????????android:text="体重" ????????????android:textSize="40dp"/> ????????<EditText ????????????android:id="@+id/W" ????????????android:text="kg" ????????????android:layout_width="200dp" ????????????android:layout_height="wrap_content" ????????????/> ????????></LinearLayout> ????//男女选项 ????<RadioGroup ????????android:id="@+id/sex" ????????android:layout_width="wrap_content" ????????android:layout_height="wrap_content" ????????android:orientation="horizontal" ????????android:layout_marginLeft="90dp" ????????android:layout_marginTop="15dp"> ????????<RadioButton ????????????android:id="@+id/man" ????????????android:layout_width="wrap_content" ????????????android:layout_height="wrap_content" ????????????android:text="男" ????????????android:textSize="30dp" ????????????/> ????????<RadioButton ????????????android:id="@+id/woman" ????????????android:layout_width="wrap_content" ????????????android:layout_height="wrap_content" ????????????android:layout_marginLeft="70dp" ????????????android:text="女" ????????????android:textSize="30dp" ????????????/> ????????></RadioGroup> ????//按钮 ????<LinearLayout ????????android:layout_width="wrap_content" ????????android:layout_height="wrap_content" ????????android:orientation="horizontal" ????????android:layout_marginLeft="90dp" ????????android:layout_marginTop="20dp"> ????????<Button ????????????android:id="@+id/b1" ????????????android:layout_width="wrap_content" ????????????android:layout_height="wrap_content" ????????????android:text="计算" ????????????android:textSize="30dp" ????????????/> ????????<Button ????????????android:id="@+id/b2" ????????????android:layout_width="wrap_content" ????????????android:layout_height="wrap_content" ????????????android:layout_marginLeft="50dp" ????????????android:text="重置" ????????????android:textSize="30dp" ????????????/> ????????></LinearLayout>
</LinearLayout>
MainActivity.java源代码:
package com.example.bim; import androidx.appcompat.app.AppCompatActivity; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; import android.view.View; import android.os.Bundle; public class MainActivity extends AppCompatActivity { ????????private Button b1,b2; ????????private RadioButton man,woman; ????????private EditText heightText,weightText,ID,PASS; ????????private TextView resText; ????@Override
????protected void onCreate(Bundle savedInstanceState) { ????????super.onCreate(savedInstanceState); ????????setContentView(R.layout.activity_main); ????????//ID = findViewById(R.id.ID); ????????//PASS = findViewById(R.id.PASS);密码和用户名没有用到 ????????//以下为变量与控件的绑定 ????????b1 = (Button) findViewById(R.id.b1); ????????heightText = (EditText) findViewById(R.id.H); ????????weightText = (EditText) findViewById(R.id.W); ????????b2 = (Button) findViewById(R.id.b2); ????????man = findViewById(R.id.man); ????????woman = findViewById(R.id.woman);
????????//button2的点击响应,作用为清空所有输入 ????????b2.setOnClickListener(new View.OnClickListener() { ????????????@Override ????????????public void onClick(View view) { ????????????????ID.setText("");//清空实质为把所有输入换成空 ????????????????PASS.setText(""); ????????????????heightText.setText(""); ????????????????weightText.setText("");
????????????} ????????}); ????????//button2的点击事件响应,计算BMI的值,结果用Toast显示 ????????b1.setOnClickListener(new View.OnClickListener() { ????????????@Override ????????????public void onClick(View view) { ????????????????//得到身高体重 ????????????????String height = heightText.getText().toString(); ????????????????String weight = weightText.getText().toString(); ????????????????double result = 0, heightNum = 0, weightNum = 0; ????????????????if(!height.isEmpty()&&!weight.isEmpty()) { ????????????????????heightNum = Double.parseDouble(height); ????????????????????weightNum = Double.parseDouble(weight); ????????????????????result = weightNum / (heightNum*heightNum); ????????????????} ????????????????if(man.isChecked()){//如果选择的是男性 ????????????????????if(result <= 19) ????????????????????{ ????????????????????????Toast.makeText(MainActivity.this,"体重偏低",Toast.LENGTH_SHORT).show(); ????????????????????} ????????????????????else if(result <= 25 && result > 19) ????????????????????{ ????????????????????????Toast.makeText(MainActivity.this,"健康体重",Toast.LENGTH_SHORT).show(); ????????????????????} ????????????????????else if(result <= 30 && result > 25) ????????????????????{ ????????????????????????Toast.makeText(MainActivity.this,"超重",Toast.LENGTH_SHORT).show(); ????????????????????} ????????????????????else if(result < 39 && result > 30) ????????????????????{ ????????????????????????Toast.makeText(MainActivity.this,"严重超重",Toast.LENGTH_SHORT).show(); ????????????????????} ????????????????????else { ????????????????????????Toast.makeText(MainActivity.this,"极度超重",Toast.LENGTH_SHORT).show(); ????????????????????}
????????????????} ????????????????else//选择的是女性 ????????????????{ ????????????????????if (result <= 18) { ????????????????????????Toast.makeText(MainActivity.this, "体重偏低", Toast.LENGTH_SHORT).show(); ????????????????????} else if (result <= 24 && result > 18) { ????????????????????????Toast.makeText(MainActivity.this, "健康体重", Toast.LENGTH_SHORT).show(); ????????????????????} else if (result <= 29 && result > 24) { ????????????????????????Toast.makeText(MainActivity.this, "超重", Toast.LENGTH_SHORT).show(); ????????????????????} else if (result < 38 && result > 29) { ????????????????????????Toast.makeText(MainActivity.this, "严重超重", Toast.LENGTH_SHORT).show(); ????????????????????} else { ????????????????????????Toast.makeText(MainActivity.this, "极度超重", Toast.LENGTH_SHORT).show(); ????????????????????} ????????????????} ????????????} ????????}); ???} }
|