Android studio实现个人体重指数计算

2023-11-03

Java代码

package com.example.work1;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;


    public class HeightCalculatorActivity extends Activity {
        private Button calculatorButton;
        private EditText weightEditText;
        private EditText heightEditText;
        private CheckBox manCheckBox;
        private CheckBox womanCheckBox;
        private TextView resultTextView;
        private EditText nameEditText;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.work);
            calculatorButton = (Button) findViewById(R.id.caculator);
            weightEditText = (EditText) findViewById(R.id.weight);
            heightEditText = (EditText) findViewById(R.id.height);
            manCheckBox = (CheckBox) findViewById(R.id.man);
            womanCheckBox = (CheckBox) findViewById(R.id.woman);
            resultTextView = (TextView) findViewById(R.id.result);
            nameEditText=findViewById(R.id.name);
        }

        protected void onStart() {
            super.onStart();
            registerEvent();
        }

        private void registerEvent() {
            calculatorButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    if (!weightEditText.getText().toString().trim().equals("")) {
                        if (manCheckBox.isChecked() || womanCheckBox.isChecked()) {
                            if (!heightEditText.getText().toString().trim().equals("")) {
                                Double weight = Double.parseDouble(weightEditText.getText().toString());
                                Double height = Double.parseDouble(heightEditText.getText().toString());
                                String name=nameEditText.getText().toString();
                                StringBuffer sb = new StringBuffer();
                                sb.append("评估结果\n");
                                if (manCheckBox.isChecked()) {
                                    double result = evaluateweight(height, weight);
                                    if (result < 25 && result >= 18) {
                                        sb.append(name+"先生,您的体重很正常");
                                    } else if (result < 18) {
                                        sb.append(name+"先生,您的体重偏低,需要多吃一点噢");
                                    } else if (result >= 25 && result < 30) {
                                        sb.append(name+"先生,您已超重,请多运动少吃肉");
                                    } else if (result >= 30 && result < 35) {
                                        sb.append(name+"先生,您为轻度肥胖,一定要加强锻炼");
                                    } else if (result >= 35 && result < 40) {
                                        sb.append(name+"先生,您是中度肥胖,请加强锻炼");
                                    } else if(result>=40){
                                        sb.append(name+"先生,您也太胖了吧,您是重度肥胖");
                                    }
                                }
                                if (womanCheckBox.isChecked()) {
                                    double result = evaluateweight(height, weight);
                                    if (result < 25 && result >= 18) {
                                        sb.append(name+"女士,您的体重很正常");
                                    } else if (result < 18) {
                                        sb.append(name+"女士,您的体重偏低,需要多吃一点噢");
                                    } else if (result >= 25 && result < 30) {
                                        sb.append(name+"女士,您已超重,请多运动少吃肉");
                                    } else if (result >= 30 && result < 35) {
                                        sb.append(name+"女士,您为轻度肥胖,一定要加强锻炼");
                                    } else if (result >= 35 && result < 40) {
                                        sb.append(name+"女士,您是中度肥胖,请加强锻炼");
                                    } else {
                                        sb.append(name+"女士,您也太胖了吧,您是重度肥胖");
                                    }
                                }
                                resultTextView.setText(sb.toString());
                            } else {
                                showMessage("请输入身高");
                            }
                        } else {
                            showMessage("请选择性别");
                        }
                    } else {
                        showMessage("请输入体重");
                    }
                }
            });


        }

        private double evaluateweight(double height, double weight) {
            double res;
            res = weight / (height * height);
            return res;
        }

        private void showMessage(String message)
        {
            AlertDialog alert=new AlertDialog.Builder(this).create();
            alert.setTitle("系统信息");
            alert.setMessage(message);
            alert.setButton("确定",new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog,int whichButton)
                {

                }
        });
            alert.show();//显示窗口
        }
        public boolean onCreateOptionsMenu(Menu menu){
            menu.add(Menu.NONE,1,Menu.NONE,"退出");
            return super.onCreateOptionsMenu(menu);
        }
        public boolean onOptionsItemSelected(MenuItem item)
        {
            switch(item.getItemId()){
                case 1:
                    finish();
                    break;
            }
            return super.onOptionsItemSelected(item);
        }
    }





xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@mipmap/kebe">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20.0dip"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="个人体重指数计算器"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10.0dip"
        android:orientation="horizontal">

        <TextView
            android:layout_width="120dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:text="请输入你的姓名:"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/name"
            android:layout_width="150.0dip"
            android:layout_height="wrap_content"
            android:inputType="text" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10.0dip"
        android:orientation="horizontal">

        <TextView
            android:layout_width="120dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:text="请输入你的体重:"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/weight"
            android:layout_width="150.0dip"
            android:layout_height="wrap_content"
            android:inputType="number|numberDecimal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="kg" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10.0dip"
        android:orientation="horizontal">

        <TextView
            android:layout_width="120dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:text="请输入你的身高:"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/height"
            android:layout_width="150.0dip"
            android:layout_height="wrap_content"
            android:inputType="number|numberDecimal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="m" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <TextView
            android:layout_width="120.0dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5.0dip"
            android:text="请选择您的性别" />

        <CheckBox
            android:id="@+id/man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男">

        </CheckBox>

        <CheckBox
            android:id="@+id/woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女">

        </CheckBox>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/caculator"
            android:layout_width="200.0dip"
            android:layout_height="wrap_content"
            android:text="运算" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/result"
            android:layout_marginTop="10.0dip"
            android:textColor="#FF00FF"
            android:textSize="20dp"/>
    </LinearLayout>


</LinearLayout>

实验样例:
在这里插入图片描述

实验心得:
Java的文件名要改的和类名一致,好久没做都忘了,还有一些很粗心导致的弱智错误,就这样吧。啪啪啪。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android studio实现个人体重指数计算 的相关文章

随机推荐

  • LIF神经元膜电压公式-迭代式推导,及其在STBP中的应用

    膜电压公式 m d u d t
  • 利用ESP定律进行脱壳

    目录 预备知识 壳的概念 UPX PEiD OllyDbg 实验目的 实验环境 实验步骤一 实验步骤二 实验步骤三 预备知识 基础的汇编语言命令以及对汇编程序的基本审计能力 壳的概念 加壳的全称应该是可执行程序资源压缩 是保护文件的常用手段
  • 抖音api开放平台对接_抖音视频API解析接口

    发布抖音视频接口一枚 无限制免费调用 但需要AppKey 更新 2019 09 23 1 视频统计信息公开 2019 06 23 1 修复无水印解析失败BUG 2 接口返回内容调整 已获取视频播放量等统计信息 目前暂未公开 接口地址 htt
  • Connect was not declared in this scope

    QT程序中的事件机制是通过SIGNAL SLOT 信号 槽 来实现的 创建一个信号与槽的连接就是使用connect方法 它是QObject类下面的一个静态方法 基本上 所有的QT对象的基类都是QObject 所以 在非QObject的派生类
  • alsa-lib应用层接口分析

    ALSA lib接口调用简介 ALSA逻辑 在我当前看来 总共有两条线 1 录放音流控 2 amixer cset控件 录放音流控 自定义名称 相当于操作OSS的 dev dsp设备 可以设置三大参数等 并且启动录放音 这里 aplay m
  • Spring Boot底层原理详解及整合

    Spring Boot框架 通过Spring Boot 可以构建一个基于Spring框架的Java Application 简化配置 自动装配 开箱即用 JavaConfiguration用Java类代替XML的配置方式 Spring Bo
  • Java实现图的深度和广度优先遍历算法

    概述 最近要学习写网络爬虫 所以把图的深度和广度搜索都再温习一下 图结构展示 实现过程 首先 我们来看看图结构在代码中的实现 有三块逻辑 1 图中的节点 java view plain copy public class GraphNode
  • MongoDB(三):MongoDB图形化界面工具

    一 下载 MongoDBCompass 官方提供的 下载地址 https www mongodb com download center compass jmp docs 二 解压 启动 三 启动MongoDB 连接MongoDB服务 出现
  • Vue笔记——Vue组件中引入jQuery

    如果想在普通的HTML页面引入jQuer库的话 直接使用即可 但是如果要在Vue组件中使用jQuery库的话 使用这样的方式就不行了 需要使用以下方法 一 安装jQuery依赖 在使用jQuery之前 我们首先要通过以下命令来安装jQuer
  • Json字符串属性里面有逗号问题

    今天小编写 代码时候 后台给我返回的一个json字符串 name group id 123 所以不能通过name group id这样来 取值 不然会报没有id属性 看到这里相信很多程序员和小编一样肯定会骂后台传递的值不对 小编也是这样骂的
  • 快速排序详解(图解实例)

    快速排序 Quicksort 是对冒泡排序的一种改进 它的基本思想是 通过一趟排序将要排序的数据 分割成独立的两部分 其中一部分的所有数据都比另外一部分的所有数据都要小 然后再按此方法 对这两部分数据分别进行快速排序 整个排序过程可以递归进
  • 红外人体感应传感器SR602模块使用说明

    一 HC SR602模块 红外人体感应传感器HC SR602是基于红外线技术的自动控制模块 专用于感应周围人体的存在 该模块相较于HC SR501 灵敏度较高 抗干扰能力大 且简单易用 二 HC SR602模块主要参数 工作电压 3 3V
  • 使用手柄控制Unity及效果展示(1)

    Unity支持手柄的控制 效果图如下所示 这是一篇针对手柄控制U3D入门的过程记载 主要以实现功能为目的 分四个部分进行过程展示 Input System包的下载 设备的查找 Input Actions控件的使用 主要代码的解释及编写 这里
  • 【css】落叶飘动效果,点击落叶飘动停止

    做项目有个需求是要求做落叶下落效果 做了一下 整体是依靠css中animation属性来控制的 keyframes move控制动画轨迹 使用伪元素checked控制动画运行和暂停 css3文档中有这样的例子 利用伪元素checked控制动
  • python 删除一个字符串内重复的内容

    使用py将 a dai liu dai 去掉重复的dai a dai liu dai a list a split 将字符串拆分成列表 a set set a list 将列表转换为集合 去除重复的元素 result join a set
  • jest搭建vue项目单元测试-vue-cli创建新项目

    说到项目会分为新建的醒目和老项目两种 我们先来说新项目 新项目加入jest单元测试 1 创建项目 使用vue脚手架创建项目 test vue jest vue create test vue jest 2 创建项目过程中配置选项 在配置项中
  • OK彻底解决ping主机ping虚拟机之间ping不通的问题

    时间轴 一个月前 2022 8 重新玩虚拟机 因为计算机网络这块不扎实 知识点模糊 不懂其中各种原由 当然现在也不是很明白 后续还需要系统的回顾 在那是一直有几个问题 遇到以下问题需要解决 1 怎么选择桥接 nat的连接方式 现在也不清楚
  • 第七章 单行函数

    MySQL系列文章目录 http t csdn cn YTPe9 文章目录 MySQL系列文章目录 前言 一 函数的理解 1 什么是函数 2 不同DBMS函数的差异 3 MySQL的内置函数及分类 二 数值函数 1 基本函数 2 角度与弧度
  • C 语言char类型与int类型的转化

    目录 一 char转int 法一 直接转换 ASSCII编码表 ASCII可显示字符 法二 利用库函数转换 二 数字换成字符串 1 用sprintf 2 用库函数 char和int的转换有两种方式 这两种方式适合于在输出时使用 一 char
  • Android studio实现个人体重指数计算

    Java代码 package com example work1 import android app Activity import android app AlertDialog import android content Dialo