Android BMI程序设计

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="left"

android:text="体重(KG)"

android:textSize="25sp"

/>

<EditText

android:id="@+id/tz"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="请输入你的体重:"

/>

<Button

android:id="@+id/btn1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="计算BMI"

android:textSize="30sp"

/>

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="left"

android:text="你的BMI总数为"

android:textSize="25sp"

/>

<EditText

android:id="@+id/BMI"

android:layout_width

="match_parent"

android:layout_height="wrap_content"/>

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/bmi"

/>

Java功能实现运行代码

调用获得xml布局文件的一系列属性,对输入的身高和体重进行运算并把结果给set到控件BIM行上,以及运用Toast进行短暂的提示!

package com.example.bmiapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private Button btn1;

private EditText heightText,weightText,resText;

// private TextView resText;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn1 = (Button) findViewById(R.id.btn1);

heightText = (EditText) findViewById(R.id.sg);

weightText = (EditText) findViewById(R.id.tz);

resText = (EditText) findViewById(R.id.BMI);

btn1.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);

TextView BMI = (TextView) findViewById(R.id.BMI);

BMI.setText("Your BMI is " + result);

}

if (result <= 18.4) {

Toast.makeText(MainActivity.this, "你的体重有点偏瘦哦", Toast.LENGTH_SHORT).show();

} else if (result<= 23.9 && result> 18.5) {

Toast.makeText(MainActivity.this, "你的体重正常哦", Toast.LENGTH_SHORT).show();

} else if (result<= 27.9 && result> 24) {

Toast.makeText(MainActivity.this, "你的体重有点过重哦", Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(MainActivity.this, "你的体重有点肥胖哦", Toast.LENGTH_SHORT).show();

}

相关推荐
用户69371750013845 分钟前
24.Kotlin 继承:调用超类实现 (super)
android·后端·kotlin
TTGGGFF17 分钟前
开源项目分享 : Gitee热榜项目 2025-12-4 日榜
gitee·开源
00后程序员张38 分钟前
Fastlane 结合 开心上架,构建跨平台可发布的 iOS 自动化流水线实践
android·运维·ios·小程序·uni-app·自动化·iphone
游戏开发爱好者844 分钟前
iOS 性能测试的工程化方法,构建从底层诊断到真机监控的多工具测试体系
android·ios·小程序·https·uni-app·iphone·webview
帅得不敢出门1 小时前
Android11~13 Framework实现Ntp服务器多域名轮询同步时间
android·服务器·python·framework·github
2501_916008891 小时前
iOS App 混淆的真实世界指南,从构建到成品 IPA 的安全链路重塑
android·安全·ios·小程序·uni-app·cocoa·iphone
百锦再1 小时前
.NET到Java的终极迁移指南:最快转型路线图
android·java·开发语言·python·rust·go·.net
喵霓2 小时前
mac—android-platform-tools介绍
android·macos
zore_c2 小时前
【C语言】文件操作详解2(文件的顺序读写操作)
android·c语言·开发语言·数据结构·笔记·算法·缓存