体质评估计算器-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_cal

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import androidx.appcompat.app.AlertDialog
import kotlin.math.abs


class 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 = 0
                    if (rb2.isChecked) {
                        subValue = 1
                    }
                    val dreamWeight = (ht - subLt[subValue]) * rateLt[subValue]
                    val overWeight = (wt - dreamWeight) / dreamWeight

                    println("dreamWeight: $dreamWeight $overWeight")

                    val tipList = arrayOf(
                        "体重不足, 体重偏轻20%以上", "体重过轻, 体重偏轻10%到20%",
                        "正常体重, 体重正负10%", "体重过重, 体重偏重10%到20%", "肥胖的小可爱,体重偏重20%以上"
                    )
                    var tipIndex = 2 // 初始设置为正常体重嘻嘻嘻
                    when {
                        overWeight < -0.2 -> tipIndex = 0
                        overWeight in -0.2..-0.1 -> tipIndex = 1
                        overWeight in 0.1..0.2 -> tipIndex = 3
                        overWeight > 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">

    <TextView
        android: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" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="40dp">

        <TextView
            android:id="@+id/txt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/txt_kg" />

        <EditText
            android:id="@+id/kg"
            android:layout_width="100dp"
            android:inputType="number"
            android:layout_height="wrap_content" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="40dp">

        <TextView
            android:id="@+id/txt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/txt_height" />

        <EditText
            android:id="@+id/height"
            android:layout_width="100dp"
            android:inputType="number"
            android:layout_height="wrap_content"
            />
    </LinearLayout>

    <RadioGroup
        android: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">

        <RadioButton
            android: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" />

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female"

            />
    </RadioGroup>

    <Button
        android: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"/>


    <TextView
        android: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"/>

    <TextView
        android: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">

    <application
        android: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 30
    buildToolsVersion "30.0.1"
    defaultConfig {
        applicationId "com.example.elevator_cal"
        minSdkVersion 15
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles 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'
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值