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

}

相关推荐
前端赵哈哈1 小时前
初学者入门:Android 实现 Tab 点击切换(TabLayout + ViewPager2)
android·java·android studio
一条上岸小咸鱼4 小时前
Kotlin 控制流(二):返回和跳转
android·kotlin
Jasonakeke4 小时前
【重学 MySQL】九十二、 MySQL8 密码强度评估与配置指南
android·数据库·mysql
Mertrix_ITCH4 小时前
在 Android Studio 中修改 APK 启动图标(2025826)
android·ide·android studio
荏苒追寻4 小时前
Android OpenGL基础1——常用概念及方法解释
android
人生游戏牛马NPC1号5 小时前
学习 Android (十七) 学习 OpenCV (二)
android·opencv·学习
恋猫de小郭5 小时前
谷歌开启 Android 开发者身份验证,明年可能开始禁止“未经验证”应用的侧载,要求所有开发者向谷歌表明身份
android·前端·flutter
用户095 小时前
Gradle声明式构建总结
android
用户096 小时前
Gradle插件开发实践总结
android
Digitally16 小时前
如何将视频从安卓设备传输到Mac?
android·macos