Android小项目---BIM体质指数计算器
生活随笔
收集整理的這篇文章主要介紹了
Android小项目---BIM体质指数计算器
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
BMI體質(zhì)指數(shù)計算器
- 一、學(xué)習(xí)提示
- 1.1、項目目標:開發(fā)一款體質(zhì)指數(shù)計算器,實現(xiàn)輸入身高和體重即可判定體型是否正常。
- 1.2、知識點:Activity;布局;Widget組件(EditText/Button/TextView);屬性菜單;Intent。
- 1.3、技能目標:能使用ADT可視化布局設(shè)計器設(shè)計基本的程序界面;在開發(fā)過程中建立重構(gòu)項目代碼的意識。
- 二、項目
- 2.1 、BMI界面設(shè)計
- 相關(guān)組件:3個TextView,兩個用來輸入浮點小數(shù)的EditText,一個Button
- 2.2、BMI功能實現(xiàn)
- 2.2.1原理分析:Body Mass Index即“體質(zhì)指數(shù)”,計算方法:體重除以身高的平方,其公式為:體質(zhì)指數(shù)(BMI)=體重(kg)/(身高(m)*身高(m))。
- 2.2.2實現(xiàn)代碼
- (1)成員變量定義,他們是程序界面上的控件/組件,需要在代碼中用到
- (2)在OnCreate()方法中設(shè)置當(dāng)前Activity顯示的界面,來自前面設(shè)計好的xml格式的布局文件
- (3)找到OnCreate()方法中在setContentView()方法下加入初始化控件
- (4)在OnCreate()方法中編寫計算按鈕的單擊事件代碼
- 2.3、BMI重構(gòu)
一、學(xué)習(xí)提示
1.1、項目目標:開發(fā)一款體質(zhì)指數(shù)計算器,實現(xiàn)輸入身高和體重即可判定體型是否正常。
1.2、知識點:Activity;布局;Widget組件(EditText/Button/TextView);屬性菜單;Intent。
1.3、技能目標:能使用ADT可視化布局設(shè)計器設(shè)計基本的程序界面;在開發(fā)過程中建立重構(gòu)項目代碼的意識。
二、項目
2.1 、BMI界面設(shè)計
相關(guān)組件:3個TextView,兩個用來輸入浮點小數(shù)的EditText,一個Button
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignTop="@+id/editHeight"android:layout_marginLeft="16dp"android:text="身高(cm)" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_alignRight="@+id/textView1"android:layout_marginTop="16dp"android:text="體重(KG)" /><EditTextandroid:id="@+id/editWeight"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/button"android:layout_alignTop="@+id/textView2"android:ems="10"android:inputType="numberDecimal" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/editHeight"android:layout_marginTop="30dp"android:text="速速算出小美女的體質(zhì)指數(shù)來" /><EditTextandroid:id="@+id/editResult"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/button"android:layout_below="@+id/button"android:layout_marginTop="82dp"android:ems="10"android:inputType="numberDecimal" /><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@+id/editResult"android:layout_alignLeft="@+id/editResult"android:layout_marginBottom="49dp"android:text="鑒定結(jié)果" /><EditTextandroid:id="@+id/editHeight"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/editWeight"android:layout_below="@+id/editWeight"android:layout_marginTop="22dp"android:ems="10"android:inputType="numberDecimal" /></RelativeLayout>2.2、BMI功能實現(xiàn)
2.2.1原理分析:Body Mass Index即“體質(zhì)指數(shù)”,計算方法:體重除以身高的平方,其公式為:體質(zhì)指數(shù)(BMI)=體重(kg)/(身高(m)*身高(m))。
2.2.2實現(xiàn)代碼
(1)成員變量定義,他們是程序界面上的控件/組件,需要在代碼中用到
//身高輸入框,體重輸入框,計算按鈕,體型顯示結(jié)果private EditText editHeight;private EditText editWeight;private Button bthCalc;private TextView textResult;(2)在OnCreate()方法中設(shè)置當(dāng)前Activity顯示的界面,來自前面設(shè)計好的xml格式的布局文件
setContentView(R.layout.activity_main);(3)找到OnCreate()方法中在setContentView()方法下加入初始化控件
editHeight=(EditText) findViewById(R.id.editHeight);editWeight=(EditText) findViewById(R.id.editWeight);bthCalc=(Button) findViewById(R.id.button);textResult=(TextView) findViewById(R.id.editResult);(4)在OnCreate()方法中編寫計算按鈕的單擊事件代碼
//響應(yīng)按鈕事件bthCalc.setOnClickListener(new OnClickListener(){ @Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubtry{double h=Double.parseDouble(editHeight.getText().toString())/100;double w=Double.parseDouble(editWeight.getText().toString());//計算BIMdouble bmi=w/(h*h);if(bmi>18.5){textResult.setText("你的體型偏瘦,需要多吃點");}else if(bmi>24.9){textResult.setText("你的體型偏胖,需要少吃點");}else{textResult.setText("你的體型不錯喲,繼續(xù)保持");}}catch(Exception e){Toast.makeText(MainActivity.this, "提示:輸入有誤", Toast.LENGTH_SHORT).show();}}});2.3、BMI重構(gòu)
發(fā)現(xiàn):在設(shè)計界面上每個控件上會顯示一個黃色的感嘆號小圖標,或者在xml布局文件中將鼠標移至黃色波浪線也會有黃色的提示信息。
問題:不應(yīng)該在界面布局文件中“硬編碼字符串”,也就是不應(yīng)該使用字符串常量,而應(yīng)該使用字符串資源,也是為了將來程序的“國際化”考慮的。
解決:對BMI項目進行重構(gòu),重構(gòu)的目的:把硬編碼的文字內(nèi)容轉(zhuǎn)移到Android資源中去
總結(jié)
以上是生活随笔為你收集整理的Android小项目---BIM体质指数计算器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对象关系映射(ORM)介绍理解
- 下一篇: E类直流-直流变换器 Matlab si