体质评估计算器-kotlin

2024-02-26 17:10
文章标签 评估 kotlin 计算器 体质

本文主要是介绍体质评估计算器-kotlin,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

这学期上了移动智能开发,做了几个作业,虽然做得不是很理想,但是还是想记录一下,方便有些用法以后回来可查。
android下载和配置见:android studio配置介绍 。

关键代码: Android-class/elevator_cal (gitee仓库)
原创链接: 体质评估计算器-kotlin
https://blog.csdn.net/weixin_43850253/article/details/113560574



效果

在这里插入图片描述
在这里插入图片描述


项目结构

主要是MainActivity.kt 和 activity_main.xml文件
在这里插入图片描述



详细代码

MainActivity.kt

package com.example.elevator_calimport androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import androidx.appcompat.app.AlertDialog
import kotlin.math.absclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)btn.setOnClickListener {if (!kg.text.toString().trim().equals("") && !height.text.toString().trim().equals("")) {if (rb1.isChecked || rb2.isChecked) {val wt = kg.text.toString().toFloat()val ht = height.text.toString().toFloat()val subLt = arrayOf(80, 70)val rateLt = arrayOf(0.7, 0.6)var subValue = 0if (rb2.isChecked) {subValue = 1}val dreamWeight = (ht - subLt[subValue]) * rateLt[subValue]val overWeight = (wt - dreamWeight) / dreamWeightprintln("dreamWeight: $dreamWeight $overWeight")val tipList = arrayOf("体重不足, 体重偏轻20%以上", "体重过轻, 体重偏轻10%到20%","正常体重, 体重正负10%", "体重过重, 体重偏重10%到20%", "肥胖的小可爱,体重偏重20%以上")var tipIndex = 2 // 初始设置为正常体重嘻嘻嘻when {overWeight < -0.2 -> tipIndex = 0overWeight in -0.2..-0.1 -> tipIndex = 1overWeight in 0.1..0.2 -> tipIndex = 3overWeight > 0.2 -> tipIndex = 4}showResultView.text = tipList[tipIndex]} else {showDialog("请选择性别") // 用AlertDialog实现函数}} else {showDialog("请输入身高或体重!") // 用AlertDialog实现函数    }}}}private fun showDialog(message_str: String) {val builder = AlertDialog.Builder(this)builder.setTitle("错误提示消息").setMessage(message_str).setPositiveButton("确定", { dialog, id -> }).setNegativeButton("取消", { dialog, id -> })builder.create()builder.show()}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/txt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="40dp"android:text="@string/title"android:textSize="30sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="30dp"android:layout_marginStart="30dp"android:layout_marginTop="40dp"><TextViewandroid:id="@+id/txt1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/txt_kg" /><EditTextandroid:id="@+id/kg"android:layout_width="100dp"android:inputType="number"android:layout_height="wrap_content" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="30dp"android:layout_marginStart="30dp"android:layout_marginTop="40dp"><TextViewandroid:id="@+id/txt2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/txt_height" /><EditTextandroid:id="@+id/height"android:layout_width="100dp"android:inputType="number"android:layout_height="wrap_content"/></LinearLayout><RadioGroupandroid:id="@+id/rg"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginLeft="30dp"android:layout_marginStart="30dp"android:layout_marginTop="40dp"><RadioButtonandroid:id="@+id/rb1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="30dp"android:checked="true"android:text="@string/radio_male" /><RadioButtonandroid:id="@+id/rb2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/radio_female"/></RadioGroup><Buttonandroid:id="@+id/btn"android:layout_width="180dp"android:layout_height="wrap_content"android:text="@string/cal_button"android:layout_gravity="center"android:layout_marginTop="30dp"/><TextViewandroid:id="@+id/resultTip"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="40dp"android:textSize="28sp"android:text="@string/result_tip"/><TextViewandroid:id="@+id/showResultView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="50dp"android:layout_marginStart="50dp"android:layout_marginTop="20dp"android:textSize="22sp"/></LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.elevator_cal"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

在build.gradle中:

apply plugin: 'com.android.application'apply plugin: 'kotlin-android'
// 这里可以方便直接按id获取组件
apply plugin: 'kotlin-android-extensions'android {compileSdkVersion 30buildToolsVersion "30.0.1"defaultConfig {applicationId "com.example.elevator_cal"minSdkVersion 15targetSdkVersion 30versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}
}dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"implementation 'androidx.appcompat:appcompat:1.0.2'implementation 'androidx.core:core-ktx:1.0.2'implementation 'androidx.constraintlayout:constraintlayout:1.1.3'testImplementation 'junit:junit:4.12'androidTestImplementation 'androidx.test.ext:junit:1.1.0'androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

想引起注意的是, 以下语句可以方便直接按id选中组件。

apply plugin: 'kotlin-android-extensions'

这篇关于体质评估计算器-kotlin的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/weixin_43850253/article/details/113560574
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/749573

相关文章

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

Kotlin运算符重载函数及作用场景

《Kotlin运算符重载函数及作用场景》在Kotlin里,运算符重载函数允许为自定义类型重新定义现有的运算符(如+-…)行为,从而让自定义类型能像内置类型那样使用运算符,本文给大家介绍Kotlin运算... 目录基本语法作用场景类对象数据类型接口注意事项在 Kotlin 里,运算符重载函数允许为自定义类型重

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

kotlin的函数forEach示例详解

《kotlin的函数forEach示例详解》在Kotlin中,forEach是一个高阶函数,用于遍历集合中的每个元素并对其执行指定的操作,它的核心特点是简洁、函数式,适用于需要遍历集合且无需返回值的场... 目录一、基本用法1️⃣ 遍历集合2️⃣ 遍历数组3️⃣ 遍历 Map二、与 for 循环的区别三、高

kotlin中的数据转换方法(示例详解)

《kotlin中的数据转换方法(示例详解)》这篇文章介绍了Kotlin中将数字转换为字符串和字符串转换为数字的多种方法,包括使用`toString()`、字符串模板、格式化字符串、处理可空类型等,同时... 目录1. 直接使用 toString() 方法2. 字符串模板(自动转换)3. 格式化字符串(控制输

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

kotlin中的模块化结构组件及工作原理

《kotlin中的模块化结构组件及工作原理》本文介绍了Kotlin中模块化结构组件,包括ViewModel、LiveData、Room和Navigation的工作原理和基础使用,本文通过实例代码给大家... 目录ViewModel 工作原理LiveData 工作原理Room 工作原理Navigation 工