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

}

相关推荐
m0_7482331714 小时前
PHP5.2十大经典特性回顾
android
草莓熊Lotso14 小时前
脉脉独家【AI创作者xAMA第二期】| 从拼图游戏到AI设计革命
android·开发语言·c++·人工智能·脉脉
肖。354878709415 小时前
html选择页最简模板源码,用于集合很多网页!游戏大全数字导航页面,数字选择页制作灵感,已经压缩到最小,现代,讲解。
android·java·javascript·css·html
m0_7482331716 小时前
PHP7.4重磅特性全解析
android
蜗牛、Z16 小时前
Android 日常开发Adb常用命令附档
android·adb
感谢地心引力1 天前
安卓、苹果手机无线投屏到Windows
android·windows·ios·智能手机·安卓·苹果·投屏
优雅的潮叭1 天前
cud编程之 reduce
android·redis·缓存
2601_949613021 天前
flutter_for_openharmony家庭药箱管理app实战+用药知识详情实现
android·javascript·flutter
一起养小猫1 天前
Flutter for OpenHarmony 实战 表单处理与验证完整指南
android·开发语言·前端·javascript·flutter·harmonyos
2601_949975081 天前
flutter_for_openharmony城市井盖地图app实战+附近井盖实现
android·flutter