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

}

相关推荐
李斯维2 小时前
循序渐进 Android Binder(二):传递自定义对象和 AIDL 回调
android·java·android studio
androidwork2 小时前
OkHttp 3.0源码解析:从设计理念到核心实现
android·java·okhttp·kotlin
像风一样自由3 小时前
【001】frida API分类 总览
android·frida
casual_clover3 小时前
Android 之 kotlin 语言学习笔记四(Android KTX)
android·学习·kotlin
移动开发者1号4 小时前
Android 大文件分块上传实战:突破表单数据限制的完整方案
android·java·kotlin
移动开发者1号4 小时前
单线程模型中消息机制解析
android·kotlin
每次的天空7 小时前
Android第十五次面试总结(第三方组件和adb命令)
android
追随远方7 小时前
Android音频开发:Speex固定帧与变长帧编解码深度解析
android·音视频
消失的旧时光-19437 小时前
Android和硬件通信
android
0wioiw07 小时前
安卓基础(编译.Class)
android