建立BMI範例程式
講授:洪國龍老師
回首頁
BMI介紹
BMI畫面設計
BMI程式設計
BMI介紹
什麼是BMI?
BMI:身高體重指數(又稱身體質量指數,英文為Body Mass Index)。
計算公式:體重(kg) / 身高(m) * 身高(m) 。
BMI值的意義
代表一個成人肥胖的標準。
BMI<18.5 為過輕,18.5≦BMI<24 為正常體重,24≦BMI<27 為過重,BMI≧27 即為肥胖。
[ 回頂端 ]
BMI畫面設計
BMI範例程式的UI分析
線性排版: LinearLayout
項目、結果、建議: TextView
輸入框: EditText
按鈕 : Button
EditText元件
可輸入文字的元件,此元件繼承TextView,因此也具TextView的相關屬性。
重要屬性:
id
:文字(例如:weight)
hint
: 資源 id (ex:@string/test)、文字
textColorHint
: 資源 id (ex:@color/red)、RGB、ARGB
inputType
: text(任何字元), textMultiLine, textEmailAddress, textPassword…
程式範例:
EditText etWeight = findViewById(R.id.weight);
String strWeight = etWeight.getText().toString();
float weight = Float.parseFloat(strWeight);
UI資源統整及優化
使用Android studio新增字串資源
字串:strings.xml
顏色:colors.xml
尺寸:dimens.xml
[ 回頂端 ]
BMI程式設計
畫面佈局檔:bmi_main.xml
程式設計:MainActivity.java
執行結果
[ 回頂端 ]