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();

}

相关推荐
还鮟4 小时前
CTF Web的数组巧用
android
小蜜蜂嗡嗡5 小时前
Android Studio flutter项目运行、打包时间太长
android·flutter·android studio
aqi005 小时前
FFmpeg开发笔记(七十一)使用国产的QPlayer2实现双播放器观看视频
android·ffmpeg·音视频·流媒体
zhangphil7 小时前
Android理解onTrimMemory中ComponentCallbacks2的内存警戒水位线值
android
你过来啊你7 小时前
Android View的绘制原理详解
android
移动开发者1号10 小时前
使用 Android App Bundle 极致压缩应用体积
android·kotlin
移动开发者1号10 小时前
构建高可用线上性能监控体系:从原理到实战
android·kotlin
ii_best15 小时前
按键精灵支持安卓14、15系统,兼容64位环境开发辅助工具
android
美狐美颜sdk15 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
恋猫de小郭20 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin