Android小项目---BIM体质指数计算器
生活随笔
收集整理的這篇文章主要介紹了
Android小项目---BIM体质指数计算器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
BMI體質指數計算器
- 一、學習提示
- 1.1、項目目標:開發一款體質指數計算器,實現輸入身高和體重即可判定體型是否正常。
- 1.2、知識點:Activity;布局;Widget組件(EditText/Button/TextView);屬性菜單;Intent。
- 1.3、技能目標:能使用ADT可視化布局設計器設計基本的程序界面;在開發過程中建立重構項目代碼的意識。
- 二、項目
- 2.1 、BMI界面設計
- 相關組件:3個TextView,兩個用來輸入浮點小數的EditText,一個Button
- 2.2、BMI功能實現
- 2.2.1原理分析:Body Mass Index即“體質指數”,計算方法:體重除以身高的平方,其公式為:體質指數(BMI)=體重(kg)/(身高(m)*身高(m))。
- 2.2.2實現代碼
- (1)成員變量定義,他們是程序界面上的控件/組件,需要在代碼中用到
- (2)在OnCreate()方法中設置當前Activity顯示的界面,來自前面設計好的xml格式的布局文件
- (3)找到OnCreate()方法中在setContentView()方法下加入初始化控件
- (4)在OnCreate()方法中編寫計算按鈕的單擊事件代碼
- 2.3、BMI重構
一、學習提示
1.1、項目目標:開發一款體質指數計算器,實現輸入身高和體重即可判定體型是否正常。
1.2、知識點:Activity;布局;Widget組件(EditText/Button/TextView);屬性菜單;Intent。
1.3、技能目標:能使用ADT可視化布局設計器設計基本的程序界面;在開發過程中建立重構項目代碼的意識。
二、項目
2.1 、BMI界面設計
相關組件:3個TextView,兩個用來輸入浮點小數的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="速速算出小美女的體質指數來" /><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="鑒定結果" /><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功能實現
2.2.1原理分析:Body Mass Index即“體質指數”,計算方法:體重除以身高的平方,其公式為:體質指數(BMI)=體重(kg)/(身高(m)*身高(m))。
2.2.2實現代碼
(1)成員變量定義,他們是程序界面上的控件/組件,需要在代碼中用到
//身高輸入框,體重輸入框,計算按鈕,體型顯示結果private EditText editHeight;private EditText editWeight;private Button bthCalc;private TextView textResult;(2)在OnCreate()方法中設置當前Activity顯示的界面,來自前面設計好的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()方法中編寫計算按鈕的單擊事件代碼
//響應按鈕事件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("你的體型不錯喲,繼續保持");}}catch(Exception e){Toast.makeText(MainActivity.this, "提示:輸入有誤", Toast.LENGTH_SHORT).show();}}});2.3、BMI重構
發現:在設計界面上每個控件上會顯示一個黃色的感嘆號小圖標,或者在xml布局文件中將鼠標移至黃色波浪線也會有黃色的提示信息。
問題:不應該在界面布局文件中“硬編碼字符串”,也就是不應該使用字符串常量,而應該使用字符串資源,也是為了將來程序的“國際化”考慮的。
解決:對BMI項目進行重構,重構的目的:把硬編碼的文字內容轉移到Android資源中去
總結
以上是生活随笔為你收集整理的Android小项目---BIM体质指数计算器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对象关系映射(ORM)介绍理解
- 下一篇: 【Android】Nexus 5X 环境