BMI体重指数计算器
最近在自学android,先练手做一个小项目BMI。顺便了解一下如何写博客,如何使用GitHub,第一次写博客,有不好的地方还请大神赐教。BMI指数(身体质量指数,简称体质指数,又称体重指数,英文为Body Mass Index,简称BMI)是用体重公斤数除以身高米数平方得出的数字。
BMI指数计算
体质指数(BMI)=体重(kg)÷身高^2(m)
BMI值 | 身体状况 |
---|---|
bmi<18.5 | 过轻 |
18.5<=bmi<24 | 健康体重 |
24<=bmi<28 | 超重 |
bmi>=28 | 肥胖 |
好了,了解了BMI之后开始正式写代码。
其实做这个项目大体就需要两步,第一步是编写界面,第二步是按钮的点击事件。
首先先建一个BMI的android项目。
先来几张app截图
现在开始我们的项目之旅~
编写界面
首先先要写这个界面,我们可以采用线性布局 ,垂直分布,其中身高和体重那里又需要一个线性布局,要把三个控件水平排放,好,直接上代码。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.mjy_bmi.MainActivity" ><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_gravity="center_horizontal"android:layout_marginTop="20dp"android:text="@string/title"android:textColor="#ff00ff"android:textSize="30dp" /><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp" ><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="50dp"android:layout_gravity="center"android:layout_weight="1"android:text="身高:"android:textColor="#ffaa00"android:textSize="20dp" /><EditText
android:id="@+id/height"android:layout_width="30dp"android:layout_height="wrap_content"android:layout_weight="1"android:layout_gravity="center"android:textSize="20dp" /><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="30dp"android:layout_weight="1"android:text="m"android:layout_gravity="center"android:textColor="#ffaa00"android:textSize="20dp" /></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp" ><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="50dp"android:layout_gravity="center"android:layout_weight="1"android:text="体重:"android:textColor="#bbaa00"android:textSize="20dp" /><EditText
android:id="@+id/weight"android:layout_width="30dp"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_weight="1"android:textSize="20dp" /><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="30dp"android:layout_weight="1"android:text="kg"android:textColor="#bbaa00"android:layout_gravity="center"android:textSize="20dp" /></LinearLayout><Button
android:id="@+id/bmi"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:background="#aaddaa"android:text="计算BMI"android:textColor="#ccff00" /><TextView android:id="@+id/display"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:textColor="#00ff00"android:textSize="20dp"/></LinearLayout>
是不是很简单,都是一些特别简单的属性,先写一个大体的界面,然后再优化一下,给他们添加颜色,修改字体等等。
第二步,在MainActivity中按钮的点击事件的实现
package com.example.mjy_bmi;import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity {EditText height,weight;String heigh_string,weight_string;TextView display;Button bmi;private double result;private double weight_double,height_double;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//this.requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);height=(EditText) findViewById(R.id.height);weight=(EditText) findViewById(R.id.weight);display=(TextView) findViewById(R.id.display);bmi=(Button) findViewById(R.id.bmi);bmi.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根//非空判断,去掉空格if (height.getText().toString().trim().length()==0) {//添加错误信息height.setError("请输入身高");return;//直接跳出当前的类}if (weight.getText().toString().trim().length()==0) {//添加错误信息weight.setError("请输入体重");return;//直接跳出当前的类}heigh_string=height.getText().toString().trim();weight_string=weight.getText().toString().trim();height_double=Double.valueOf(heigh_string);weight_double=Double.valueOf(weight_string);result=weight_double*1.0/height_double/height_double;if(result<18.5){display.setText("你的BMI值为"+result+",呀,你太轻了,多吃点好吃的补补吧,不用担心长胖哦~");display.getText();}if(result>=18.5&&result<24){display.setText("你的BMI值为"+result+",属于健康体重哦,继续保持哦~");display.getText();}if(result>=24&&result<28){display.setText("你的BMI值为"+result+",哎呀,超重了哦,注意锻炼身体哦,使自己有一个健康的身体,加油,相信你!");display.getText();}if(result>=28){display.setText("你的BMI值为"+result+",您处于肥胖状态哦,一定要注意锻炼身体了,拥有一个健康的身体很重要哦~");display.getText();}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}
}
height,weight首先获取文本框中的值,注意他现在是字符串的形式,我们要计算的话必须把他转化成double类型。在计算之前,我们先判断身高和体重是否为串。首先了解Trim()函数,删除字符串首尾的空白(可以首尾一起,也可以指定首或尾,取决于控制参数),但会保留字符串内部作为词与词之间分隔的空格。
//非空判断,去掉空格
if (height.getText().toString().trim().length()==0) {//添加错误信息height.setError("请输入身高");return;//直接跳出当前的类}
如果不为空的话,将字符串类型转化为double类型进行计算
heigh_string=height.getText().toString().trim();
weight_string=weight.getText().toString().trim();
height_double=Double.valueOf(heigh_string);
weight_double=Double.valueOf(weight_string);
带入计算公式
result=weight_double*1.0/(height_double*height_double);
进行判断
if(result<18.5){display.setText("你的BMI值为"+result+",属于健康体重哦,继续保持哦~");display.getText();}
先设置他的文本,再显示他的文本。
好了,简简单单几步,一个小小的BMI app出世了,是不是很简单呐,当然,你还可以在此基础上继续对它进行优化。
第一次写博客,不知道写的东西好不好,以后会继续坚持下去的,每次做一个项目,直接把项目总结写到博客里面。新手上路,大牛勿喷,大家一起学习,一起进步吧!
最后附上源代码
[BMI源代码下载地址](https://github.com/mujuyan/BMI)
BMI体重指数计算器相关推荐
- python代码测试健康指数计算器_python编写的bmi体重指数计算器、出租车费用计算器、个税计算器、猜数字游戏...
''' 写四个函数 分别包含bmi体重指数计算器.出租车费用计算器.个税计算器.猜数字游戏 ''' #体重指数计算器 def bmi(): while 1: #声明变量 身高.体重 height=in ...
- 安卓bmi项目_身高体重指数计算器手机版|bmi体重指数计算器安卓版下载 v1.0.0 - 跑跑车安卓网...
bmi体重指数计算器是一款手机计算器工具软件,为用户进行身高体重的bmi计算,让你知道你的肥胖特征,让你能够知晓更好去保持好身材,快来下载使用. 软件介绍 体重指数计算器 描述 本应用程序是一款免费的 ...
- total_method写四个函数 分别包含bmi体重指数计算器、出租车费用计算器、个税计算器、猜数字游戏
'''写四个函数 分别包含bmi体重指数计算器.出租车费用计算器.个税计算器.猜数字游戏 ''' # 使用到python中的内置的random模块 # 引入random模块 import random ...
- 类的应用: 1.bmi体重指数计算器 2.taxi出租车费用计算器 3.猜数字 4.税率
1.bmi体重指数计算器 class BMI(object):def bmi(self):while True:# 声明变量 身高.体重height = input('请输入您的身高(m):')wei ...
- 【一起学Java第四期】BMI体重指数计算器
文章目录 前言 接第三期: 一.BMI具体指什么? 二.技术准备(需要学习了解的技术) 1.变量 2.标识符 2.基本符号 3.数据类型 4.数据类型转换 5.运算符 6.选择结构 一.BMI如何计算 ...
- 测知年龄用计算机,BMI身高体重指数计算器 一测便知你是否需要减肥 - 房贷计算器...
BMI身体质量指数计算器 快速计算您的身体质量(BMI), 身体质量指数 (Body Mass Index, 简称BMI), 是衡量人体胖瘦程度以及是否健康的一个标准,快来算一下您的BMI值有没有超标 ...
- 体重指数计算器(中文版)
体重指数计算器 1.C++体重指数计算器(国际标准)代码:↓ #include<bits/stdc++.h> #include<Windows.h> double zhishu ...
- JAVA——附加作业4——体重指数计算器
做一个窗体应用程序"体重指数计算器",能根据用户输入的性别.身高.体重计算出"体质指数(身体质量指数)",并给出是否偏胖的判断.其中,体质指数(BMI) =体重 ...
- 【中国大学MOOC】java程序设计-week10-做一个窗体应用程序“体重指数计算器”
1.题目 做一个窗体应用程序"体重指数计算器",能根据用户输入的性别.身高.体重计算出"体质指数(身体质量指数)",并给出是否偏胖的判断.其中,体质指数(BMI ...
- 【BMI指数计算器V1.0】项目实战
项目背景 BMI指数(即身体质量指数,简称体质指数又称体重,英文为Body Mass Index,简称BMI),是用体重公斤数除以身高米数平方得出的数字,是目前国际上常用的衡量人体胖瘦 ...
最新文章
- http://wenku.baidu.com/view/26afdb8371fe910ef12df8ccRevit采用DWG和FBX两种格式导入3D max方法的总结...
- html5怎么要文字飘起来,html分享之一个超级炫酷的-404飞起来了
- java 顺序 读写 Properties 配置文件 支持中文 不乱码
- python shell怎么调字体_Python3设置在shell脚本中自动补全功能的方法
- 2 宽度优先爬虫和带偏好的爬虫(4)
- Matlab R2010a 32bit 绿色免安装版
- iOS开发——网络使用技术OC篇网络爬虫-使用正则表达式抓取网络数据
- 图像相减的matlab仿真及光栅滤波法,图像相减的MATLAB 仿真及光栅滤波法实验实现...
- Elasticsearch笔记三之版本控制和插件
- MEncoder的基础用法—6.8. 从多个输入图像文件进行编码(JPEG, PNG, TGA等)
- 如何实现Windows Network所有会话的限制登录和访问控制
- 微信小程序报错:invalid credential, access_token is invalid or not latest
- zabbix中文乱码的三种解决办法
- Windows 虚拟机介绍以及安装系统教程
- github上比较好用的第三方库
- 【优化求解】基于收敛因子和黄金正弦指引机制的蝴蝶优化算法求解单目标优化问题matlab代码(AGSABOA)
- 用计算机弹起风了歌词,起风了歌词(买辣椒也用券演唱)
- mysql数据库 菜鸟_数据库-MySQL入门
- Distinctive Image Features from Scale-Invariant Keypoints-SIFT算法译文
- 简单短波收音机电路基于 TA7642
热门文章
- Matplotlib饼状图
- ios睡眠分析 卧床 睡眠_iPhone睡眠模式:教你设置追踪睡眠排程与提升睡眠品质...
- 计算机上分辨率怎么设置在哪里设置方法,Win7分辨率怎么调 Win7屏幕分辨率设置教程...
- html对话框取消确定,alert 确定 取消
- makefile碰到问题总结
- Mac苹果电脑上右键创建文件
- ApacheCN C# 译文集 20211124 更新
- mysql索引失效的几种情况
- VMware Workstation启动虚拟机时蓝屏
- IDEA中配置类提示Spring Boot Configuration Annotation Processor not configured