Android入门之简单的BMI计算

  • UI效果图
  • 功能
    • 知识点
  • UI代码
  • Java主体部分
  • 效果图

UI效果图

功能

1.输入体重身高,根据男女计算BMI值(用户密码是摆设,暂时不具备这个功能)
2.清空所有输入

知识点
  1. 按钮的点击响应
  2. 得到用户输入
  3. Toast用法
  4. 如何清空EditText

UI代码

UI代码部分用的全是LinearLayout布局嵌套,比较繁琐

<?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">
//用户<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginLeft="50dp"android:layout_marginTop="50dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户"android:textSize="40dp"/><EditTextandroid:id="@+id/ID"android:layout_width="200dp"android:layout_height="match_parent"/>></LinearLayout>
//密码<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginLeft="50dp"android:layout_marginTop="50dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密码"android:textSize="40dp"/><EditTextandroid:id="@+id/PASS"android:layout_width="200dp"android:layout_height="match_parent"android:inputType="numberPassword"/>></LinearLayout>
//身高        <LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginLeft="50dp"android:layout_marginTop="50dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="身高"android:textSize="40dp"/><EditTextandroid:id="@+id/H"android:text="m"android:layout_width="200dp"android:layout_height="match_parent"/>></LinearLayout>
//体重<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginLeft="50dp"android:layout_marginTop="50dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="体重"android:textSize="40dp"/><EditTextandroid:id="@+id/W"android:text="kg"android:layout_width="200dp"android:layout_height="wrap_content"/>></LinearLayout>
//男女选项<RadioGroupandroid:id="@+id/sex"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginLeft="90dp"android:layout_marginTop="15dp"><RadioButtonandroid:id="@+id/man"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"android:textSize="30dp"/><RadioButtonandroid:id="@+id/woman"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="70dp"android:text="女"android:textSize="30dp"/>></RadioGroup>
//按钮<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginLeft="90dp"android:layout_marginTop="20dp"><Buttonandroid:id="@+id/b1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="计算"android:textSize="30dp"/><Buttonandroid:id="@+id/b2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="50dp"android:text="重置"android:textSize="30dp"/>></LinearLayout></LinearLayout>

Java主体部分

package com.example.bmitext;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {private Button b1,b2;private RadioButton man,woman;private EditText heightText,weightText,ID,PASS;private TextView resText;@Overrideprotected 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() {@Overridepublic void onClick(View view) {ID.setText("");//清空实质为把所有输入换成空PASS.setText("");heightText.setText("");weightText.setText("");}});//button2的点击事件响应,计算BMI的值,结果用Toast显示b1.setOnClickListener(new View.OnClickListener() {@Overridepublic 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();}}}});}
}

效果图

Android入门之简单的BMI计算相关推荐

  1. Flutter for App——一个简单的BMI计算APP

    一个简单的BMI计算APP 效果截图 初始化 布局 顶部区域 标题 计算结果 组合顶部区域 背景 中间区域 输入框 输入行 计算按钮 分界线 组合中间区域 底部区域 页面组合 BMI计算 Toast弹 ...

  2. Android入门,简单画图板的实现,自定义组件的实现

    Android的入门开发: 1:在Eclipse中新建一个Android项目,基本流程是:新建一个项目,点击File-New-Android Project,然后在三         个框中分别输入你 ...

  3. Android入门项目:关于BMI体质指数计算器

    目标:开发一款BMI体质指数计算器.用户在主界面中输入身高和体重,单击"计算BMI值"按钮后,在界面二通过TextView显示相应的结论:界面二点击返回能够回到主界面. 体质指数与 ...

  4. Android入门之简单拨号器

    效果图: package jk.phone;import android.net.Uri; import android.os.Bundle; import android.app.Activity; ...

  5. Android入门简单吗

    手机可以说是人们形影不离的东西,出门必备的就是手机,随着安卓智能的快速发展,手机已经成为聊天.阅读.支付和拍照的必备工具.手机的大热也给Android程序员带来了未来.那Android入门简单吗? A ...

  6. android之简单的BMI计算器

    简单的BMI计算器 MainActivity.java public class MainActivity extends Activity {Spinner s1,s2;EditText e1,e2 ...

  7. Linux加法简单程序,Android应用程序的开发目录——简单的加法计算示例

    突然说写Android 的笔记让我很迷茫,不知从哪里开始写,就按老师上课的过程进行吧! Android的程序目录 代码建立在src中这个不用解释了,在gen所在的文件夹里有个叫R.java的类,这个类 ...

  8. Android入门项目(一):BMI体质指数计算器

    目标:开发一款体质指数计算器,实现输入身高体重即可判断体型是否正常 知识点:Activity:布局:基本组件的使用 BMI是体质指数,公认的一种评定个人体质肥胖程度的分级方法,具体的计算方法是以体重除 ...

  9. (4)Android入门——android四大组件基础介绍及打电话,发短信简单应用 单元测试

    1,本章是android入门最后一篇,从后面开始将进入进阶阶段.我也会加快更新速度.但愿能保持每天三篇博文的数量.但是是在保证质量的前提下.后面我还会发布一些实际开发中用到的应用案例.敬请关注! 应用 ...

最新文章

  1. appium+python自动化测试教程_Python+Appium实现自动化测试
  2. WINCE5 s3c2440_SD驱动知识补充
  3. 关于使浏览器崩溃的代码尝试
  4. 修正wme输出的ASF流数据
  5. SpringMVC原理及非注解配置详解
  6. 大话云存储,这个“对象”可能无处不在
  7. 计算机网络有哪些技能知识,网络基础知识及操作技能.ppt
  8. TensorFlow tf.random.categorical
  9. 【ACL2020】DeeBERT:衡量性能与效率的 BERT 推理方法
  10. 当U盘内的文件夹都成了1KB的快捷方式的解决方法
  11. python写web界面读取txt_web端自动化——Python读取txt文件、csv文件、xml文件
  12. 学生选课管理系统php,学生选课管理系统(全源代码.doc
  13. android5.1+xposed卡刷包,一加5 7.1 ROM刷机包 最终版王者高帧率极速吃鸡超多自定义Xposed...
  14. 科赫雪花曲线 matlab编程,科赫雪花曲线的MATLAB编程实现.doc
  15. Android P 音频焦点管理
  16. linux终端联网网速慢,解决ubuntu 上网速度慢的问题
  17. html5拖拽表单设计器,可视化表单设计器拖拽生成表单(原创)
  18. ef mysql 约定_EF 数据库连接约定(Connection String Conventions in Code First)
  19. 华为生成很多html文件,原来只要按下华为手机这个键,100页纸质文件就能立马扫描成PDF...
  20. 由于代理原因,联网失败的解决方法

热门文章

  1. 在c语言中void是什么,C语言中void是什么意义?_后端开发
  2. python3输出中文_解决Python3用PIL的ImageFont输出中文乱码的问题
  3. 计算机软件系统是由哪两个部分组成部分组成,计算机组成部分-计算机系统由哪几部分组成...
  4. .NET Core学习——Dapper
  5. CSS3transform属性详解
  6. 都在讲数据仓库,你知道它的作用是什么吗?
  7. USTC2022秋季数字图像分析考试回忆
  8. 瑞星提醒:IE漏洞(MS10-018)已感染1800万网民
  9. 【软考备战·希赛网每日一练】2023年5月4日
  10. byte b=(byte)128 b=-128转换过程