(Anroid Studio)用简单代码实现BMI计算器并且将应用安装到手机上

Android BMI程序设计 Toast.makeText(MainActivity.this, “你的体重有点偏瘦哦”, Toast.LENGTH_SHORT).show();Toast.makeText(MainActivity.this, “你的体重有点过重哦”, Toast.LENGTH_SHORT).show();Toast.makeText(MainActivity.this, “你的体重有点肥胖哦”, Toast.LENGTH_SHORT).show();android:text=“你的BMI总数为” 阅读详情

第一步:创建工程:

第二步:编写前端页面:

找到content_main.xml:

 具体代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <fragment
        android:id="@+id/nav_host_fragment_content_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="invisible"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:navGraph="@navigation/nav_graph" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="168dp"
        android:layout_height="93dp"
        android:text="@string/label"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.269" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="63dp"
        android:layout_height="26dp"
        android:layout_marginStart="52dp"
        android:layout_marginTop="32dp"
        android:text="身高(cm)"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
    <!--app代表上下左右位置-->

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="@string/tips"
        android:inputType="textPersonName"
        android:minHeight="48dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.255"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="60dp"
        android:layout_height="23dp"
        android:layout_marginStart="52dp"
        android:layout_marginTop="48dp"
        android:text="体重(kg)"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <EditText
        android:id="@+id/editTextTextPersonName2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="@string/tips2"
        android:inputType="textPersonName"
        android:minHeight="48dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="184dp"
        android:onClick="onClick"
        android:text="@string/count"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.192" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="300dp"
        android:layout_height="35dp"
        android:layout_marginTop="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName2" />
</androidx.constraintlayout.widget.ConstraintLayout>

第三步:编写后端:

找到MainActivity类:里面需要注意编写的每一个组件的id需要一致。

package com.example.myapplication777;

import android.icu.text.Edits;
import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.myapplication777.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;


    //计算
    public  float height=0f;
    public  float weight=0f;
    public  float result=0f;
    //输入框的属性
    EditText editText1=null;
    EditText editText2=null;
    TextView  textView =null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.toolbar);
        //实例化
        //身高体重
        editText1=(EditText)findViewById(R.id.editTextTextPersonName);
        editText2=(EditText)findViewById(R.id.editTextTextPersonName2);
        //显示结果
        textView=(TextView)findViewById(R.id.textView4);




        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

        binding.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
//计算
    public  void onClick(View view){
        Log.d("TAG","--------点击了计算按钮-------");
        height= Float.parseFloat(editText1.getText().toString());
        height=height/100f;
        weight= Float.parseFloat(editText2.getText().toString());
        result=weight/(height*height);
        textView.setText("您的BMI值为:"+result);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

第四步:调试

如果因为gradle版本的问题可以看前面一期。

第五步:安装到手机

 

 

 进行安装即可,出现安卓手机版本问题需要调一下sdk。最后手机上安装成功。

Android代码-BMI计算器 BMI-Calculator-for-Android An exercise app I made to train with simple Android applications. I focused on the method of data input, trying to avoid writing values by EditText. I also experimented with making it a fun app, both a calculator and a toy. http://imgur.com/a/aY0BX 立即下载

相关推荐

安卓版BMI代码

安卓版BMI(人体健康指数测试)代码,android版代码

AndroidStudio实训1——BMI计算器

一、 AndroidStudio中的线性布局(LinearLayout)多用于按钮的水平或垂直排列二、LinearLayout常用属性:布局中组件的排列方式(组件垂直排列)(组件水平排列)2、gravity:控制组件所包含的子元素的对齐方式3、**layout_gravity:**控制该组件在父容器中的对其方式4、:布局的宽度,一般不直接写数字通常android:layout_width=“match_parent”(填满容器的意思)**layout_height:**布局的高度,

qq_46161529的博客 4410

BMI体质指数计算器源代码(Eclipse)

Android开发源代码,注释都有,如有需要可以联系我给视频自学

Android Studio BMI计算器设计\三种计算标准\

Android Studio BMI计算器设计、三种计算标准 附上设计要求、设计框架、全部布局界面代码、java代码以及模拟器模拟展示结果。

qq_33168558的博客 9947

YOLOv5s模型训练全流程解析:从数据准备到实战检测

本文详细解析了YOLOv5s模型训练的全流程,涵盖从环境搭建、数据准备与格式转换,到模型训练、参数调优及实战检测的完整步骤。文章提供了具体的代码示例和常见问题解决方案,旨在帮助开发者,特别是新手,高效掌握YOLOv5s模型训练的核心技术,并成功应用于实际检测任务中。

weixin_29041767的博客 332

Unity3d SECTR 2019 从零开始了解(零)

插件刚下载下来,虽然从Demo里面看了下大致效果,但尝试使用的时候茫无头绪,因此决定一边读文档一边写下笔记。这个笔记会从最基本的名词概述开始一步一步到用法。 除非特别注明,所有内容都不是机翻,而是理解后自己翻译的。 翻译内容: 【零】API REFERENCE MANUAL第一章Documentation Core Concept 【一】 名词概述 CORE CORE是SECTR的核心,一个快速将 Sectors 和 Portals 加入到Unity中的工具,以及完整的源代码,可以构筑独特的功能。 VI

狗蛋在土里瑟瑟发抖 1500

BMI计算器(Android)

AndroidBMI体质计算器

m0_75271682的博客 1027

从Mate到Mate40 Pro+:华为旗舰核心配置演进与选购避坑指南

本文回顾了华为Mate系列从初代到Mate40 Pro+的核心配置演进历程,重点分析了处理器性能、影像系统、屏幕设计及续航快充的技术突破。针对选购需求,提供了区分版本、5G选择及存储配置的实用避坑建议,帮助消费者明智选择华为旗舰机型。

weixin_30015371的博客 494

Android studio实现计算BMI指数(两个页面之间的跳转)

BMI计算器

qq_53038201的博客 2390

Mediatek NeuroPilot Yolov8量化

【代码】Mediatek NeuroPilot Yolov8模型转化。

sdrgtywretyeruet的专栏 345

用 SE84 反查 SAP 消息号:弹窗一闪而过也能抓到 Message ID 和 Number 的小技巧

SAP开发技巧:通过消息文本反查Message ID和编号 在SAP开发中,经常遇到系统只显示错误消息文本而缺少技术信息的情况。本文介绍两种实用方法: 使用SE84工具:通过输入带通配符的短文本(如"错误文本")反向查找消息类和编号 直接查询T100表:利用消息文本在T100表中检索完整的消息技术信息 这些技巧特别适用于处理弹窗消息、框架层错误等无法直接获取技术细节的场景。掌握消息ID和编号后,开发者可以更高效地进行问题定位、代码调试和错误处理,尤其在SAP Gateway、RAP等现代

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 370

安卓程序设计作业源码——BMI计算

1.(程序题 50.0分)按图1完成界面1的设计,使用表格布局实现,设定表格布局的内边距为20dp,“BMI计算”居中显示,设置上外边距为30dp,文本大小为30sp;其他所有文本大小都为24sp,性别男默认为选中状态。点击按钮计算后,先判断身高和体重是否填写,如果没有填写,使用Toast提示,如图2所示;如己填写,则将性别、身高和体重三个数据传递至第二个界面中显示,如图3所示,界面2使用线性布局实现,设置内边距为20dp;“BMI计算结果”居中显示,文本大小为30sp,设置上外边距为30dp;

科大云炬 2373

42、多媒体与虚拟现实在艺术和影视领域的创新应用

本文探讨了多媒体与虚拟现实在艺术和影视领域的创新应用,涵盖三个典型案例:影视与人类学融合项目通过协作模式模糊作者与观众界限,推动文化文献的发展;交互式数字短片《分手》利用基于对象的广播技术探索个性化影视体验;威尼斯双年展瑞士馆4D项目则通过虚拟现实技术实现文化遗产的数字化展示。这些案例展示了技术在叙事、用户体验及文化保护中的深度应用,并展望了未来技术融合、个性化深化与文化遗产传承的发展趋势。

kappa的博客 114

在Android Studio中创建一个身高体重的BIM查询项目

tvResult.setText("您的BMI值为:" + String.format("%.2f", bmi));6. 在项目的`src/main/java`目录下,找到`MainActivity.java`文件并打开它。4. 在项目的`res/layout`目录下,找到`activity_main.xml`文件并打开它。7. 在`MainActivity.java`文件中,为计算按钮添加点击事件监听器,并在点击事件中执行BMI计算操作。2. 选择"Empty Activity"模板并点击"Next"。

WuMingHua_的博客 1851

42、InfiniBand技术详解:QP状态机、操作及错误处理

本文深入解析InfiniBand技术中的核心组件QP(Queue Pair)状态机、工作请求处理机制、支持的操作类型及错误处理策略。详细介绍了QP的创建与状态转换流程,包括INIT、RTR、RTS等关键状态,并提供了状态修改的代码示例。文章还探讨了工作请求的发布、资源使用限制、围栏机制以及重试和RNR流程,帮助开发者理解通信可靠性保障机制。此外,涵盖了多播组的应用场景与API使用方法,并对比了用户空间与内核级RDMA API的调用差异,最后给出了实践建议和常见问题解答,为构建高性能分布式系统提供技术指导。

oauth7security的博客 131

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

Android Studio计算BMI小软件

计算BMI安卓小软件 简单安卓入门小项目 开发软件:Android Studio,本软件有两个页面,一个是输入身高和体重页面,另一个是结果界面

weixin_56612338的博客 6486

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

AndroidStudio简单代码实现BMI计算

DAY1 xml代码 ```xml <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="30dp" android:text="BMI计算器" android:textSize="25dp"/> <EditText android:id="@+id

qq_46485804的博客 6194

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Android开发实用小工具十三——BMI指数计算工具

BMI指数计算工具的开发与实现。

qq_37371109的博客 3290

Android开发实践:设计安卓应用以计算BMI指数

使用Android Studio设计安卓应用,计算BMI指数(身体质量指数)计算公式:BMI=体重(kg)/ 身高²(米)

小杨的博客 4674

android 点击图片切换图片

public class MainActivity extends AppCompatActivity { private boolean flag; private ImageView iv1,iv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCr...

小牧在一直在学习,在前进的道路上大家一起学习,进步。 2252

Android|BMI体质计算器实现(附测试源码)

大家好,我是执念。本博文源于安卓基础,主要介绍如何实现BMI体质计算器。先讲规则后讲实现

执念斩长河 1万+

android入门级小软件身高体重计算器源代码,初学者可以看看~

android入门级小软件身高体重计算器源代码,初学者可以看看~ 一,BMI(身高体重计算器)源代码 package com.android.bmi; import java.text.DecimalFormat; import android.app.Activity; import android.app.AlertDialog; i

前方有只程序猿 5969

移动应用开发:实验二——使用Android Studio实现标准身高计算器

1,分析:从下图中也可发现这是一个水平布局,并且两个单选按钮是绑定在一起的(如不是共同,则性别就可以全选,就失去了单选按钮的特点,出现逻辑错误)。4,添加一个输入框控件,用于接收用户输入的数据,并且给上一个id值,后续编写Java代码接收信息。2,根据分析,此时再添加一个水平布局,二级的布局方式,与体重输入框布局属于同级。3,添加线性布局,根据效果图,页面整体布局为垂直;6,单击运行按钮,启动项目,如下效果,成功创建项目。1,根据下面效果图,这是一个水平布局,包含三个控件。

梁辰兴的博客 1642

Android小项目---BIM体质指数计算器

BMI体质指数计算器项目目标:开发一款体质指数计算器,实现输入身高和体重即可判定体型是否正常。知识点:Activity;布局;Widget组件(EditText/Button/TextView);属性菜单;Intent。技能目标:能使用ADT可视化布局设计器设计基本的程序界面;在开发过程中建立重构项目代码的意识。BMI界面设计相关组件:3个TextView,两个用来输入浮点小数的EditText,一个ButtonBMI功能实现原理分析:Body Mass Index即“体质指数”,计算方法:体重除以身高的平

Smallstupid_pig的博客 7566

Android移动应用开发-小案例1 BMI计算应用开发

练习UI组件知识

m0_70569151的博客 390
上一篇: (Android Studio)一套版本兼容的gradle插件
下一篇: (c++)王道oj小练习2
xiaoniu667
博客等级 码龄5年 91粉丝 39原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值