IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> 安卓android BMI体质指数测试项目 -> 正文阅读

[移动开发]安卓android BMI体质指数测试项目

一、 界面布局:

(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();
????????????????????}
????????????????}
????????????}
????????});
???}
}

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-09-01 12:02:26  更:2021-09-01 12:03:31 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/21 10:04:46-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码