本文主要是介绍体质评估计算器-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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!