日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android|BMI体质计算器实现(附测试源码)

發(fā)布時(shí)間:2023/12/29 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android|BMI体质计算器实现(附测试源码) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

大家好,我是執(zhí)念。本博文源于安卓基礎(chǔ),主要介紹如何實(shí)現(xiàn)BMI體質(zhì)計(jì)算器。先講規(guī)則后講實(shí)現(xiàn)

體質(zhì)計(jì)算規(guī)則

胖瘦程度體質(zhì)指數(shù)
過輕男性低于20,女性低于19
適中男性20-25,女性19-24
超重男性25-30,女性24-29
肥胖男性30-35,女性29-34
嚴(yán)重肥胖男性高于35,女性高于34

測(cè)試效果


案例打印出保留兩位BMI指數(shù),并且對(duì)于身高體重小于0,異常不處理!

實(shí)例步驟

創(chuàng)建項(xiàng)目My eleApplication


點(diǎn)進(jìn)Project—>Empty Activity—>然后名字改下,finish即可。成功之后,點(diǎn)擊箭頭運(yùn)行程序。

程序正??梢耘艹晒ello world字樣,下面我們繼續(xù)

布局activity_main.xml

大家從實(shí)驗(yàn)效果可以看出,這里用了不少控件。三個(gè)TextView,兩個(gè)EditText,兩個(gè)單選按鈕控件,一個(gè)按鈕控件,代碼下方對(duì)內(nèi)容作進(jìn)一步分析!

<?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:layout_width="wrap_content"android:layout_height="wrap_content"android:text="身高(cm)"android:id="@+id/txt1"/><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/edX"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="體重(kg)"android:id="@+id/txt2"/><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/edY"/><RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:id="@+id/rg"><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"android:id="@+id/rb1"android:layout_marginRight="30dp"android:checked="true"/><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"android:id="@+id/rb2"/></RadioGroup><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/btn"android:text="計(jì)算BMI值"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/tvResult"/></LinearLayout>

LinearLayout詳解

這是基礎(chǔ)的線性布局,只需要配置width與height。水平方向即可

TextView詳解

我們用了三個(gè),其中一個(gè)是輸出結(jié)果,另外兩個(gè)只是為了代碼更友好。具體的也是基礎(chǔ)設(shè)置。

RadioButton詳解

要想使用單選按鈕必須要設(shè)置按鈕組,然后一些基本的配置,包括text,id,width與height。id是后期獲得操作的。必須設(shè)置

EditText詳解

很多時(shí)候,編輯文本是在初學(xué)者時(shí)期需要用到的。因此id必須設(shè)置

搭建MainActivity.java代碼

java代碼在完成這個(gè)案例時(shí)只需要三步,這也是翁愷老師經(jīng)常說的三步:“輸入-處理–輸出”。輸入就是指獲取View種的所有對(duì)象。處理就是指能根據(jù)輸入值找出判斷依據(jù),輸出就是把答案拋出來。

package com.example.myeleapplication;import androidx.appcompat.app.AppCompatActivity;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView;import org.w3c.dom.Text;public class MainActivity extends Activity implements View.OnClickListener {RadioButton rb1;RadioButton rb2;TextView tvResult;EditText txt1;EditText txt2;Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setViews();}public void setViews() {rb1 = (RadioButton) findViewById(R.id.rb1);rb2 = (RadioButton) findViewById(R.id.rb2);//判斷男女tvResult = (TextView) findViewById(R.id.tvResult);txt1 = (EditText)findViewById(R.id.edX);txt2 = (EditText)findViewById(R.id.edY);btn = (Button)findViewById(R.id.btn);btn.setOnClickListener(this);}@Overridepublic void onClick(View v) {double x = Double.parseDouble(txt1.getText().toString());//身高double y = Double.parseDouble(txt2.getText().toString());//體重//體重除以身高的平方double res;String str = "Your BMI is";if(x<=0 || y<=0) {tvResult.setText("值異常,不計(jì)算");return ;}x = x/100;res = y / (x*x);String str1 = String.format("%.2f",res);str = str + str1;if(rb1.isChecked())res -= 1;//以女性為標(biāo)準(zhǔn)進(jìn)行比較//僅以女性作為評(píng)價(jià)標(biāo)準(zhǔn)str += "體型:";if(res < 19)str += "過輕";else if(res < 24)str += "適中";else if(res<29)str += "超重";else if(res<34)str += "肥胖";elsestr += "嚴(yán)重肥胖";tvResult.setText(str);} }

點(diǎn)擊運(yùn)行,獲取結(jié)果

總結(jié)

執(zhí)念并在此總結(jié)了實(shí)例的步驟:

  • 創(chuàng)建項(xiàng)目,跑通hello world
  • 搭建界面
  • 配置java代碼
  • 點(diǎn)擊運(yùn)行,收獲喜悅

很高興博文能幫助到大家!

總結(jié)

以上是生活随笔為你收集整理的Android|BMI体质计算器实现(附测试源码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。