一、程序項目架構
二、編寫“BMI身高體重指數計算器”應用程序
1、設計用戶界面
打開 “res/layout/activity_main.xml” 頁面佈局文件進行編輯
android:id="@+id/height" “@”符號是提示XML解析器應該把後面的字符串解析成標識符號,“+”符號代表將添加一個標識符號。“id/”表示這個標識符號會被歸類在“id”下面,“height”是這個界面組件的“android:id”。以後的程序中,會使用“R.id.height”來取得這個界面組件。
因此“@+id/height”的意思是我們在此創建了名爲“height”的標識符,可以通過這個標識符來控制所對應的界面組件。
以下XML佈局文件已經將字符串進行抽離
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:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".MainActivity"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/height" /> <!--android:numeric="integer"限制用戶在文本框只能輸入整數--> <!--android:text=""指定“EditText”默認顯示的文字--> <EditText android:id="@+id/height" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numeric="integer" android:text=""/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/weight" /> <!--新版本“android:inputType”代替一堆輸入屬性--> <!--“numberDecimal”表示文本框可以輸入包含小數點的數字--> <EditText android:id="@+id/weight" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="numberDecimal" android:text=""/> <Button android:id="@+id/submit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/bmi_btn"/> <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <TextView android:id="@+id/suggest" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> </LinearLayout>
將字符串抽離XML
我們將XML佈局文件中的文字描述部分抽離出來,使界面更容易維護
編輯 “res/values/string.xml” 文件,表示字符串的格式爲 <string name="標識代號">文字敘述</string>。然後,我們把這些字符串應用到之前定義好的XML佈局文件中。通過使用 @string/[標識符號]
string.xml
<resources> <string name="app_name">BMI</string> <string name="height">身高(cm)</string> <string name="weight">體重(kg)</string> <string name="bmi_btn">計算 BMI 值</string> <string name="bmi_result">你的 BMI 值是</string> </resources>
新增XML文件
在 “res/values” 目錄中新建一個 “advice.xml” 文件,文件裏面將包含 “BMI程序” 算出 “BMI值” 後將給予的建議文字。完整的文件如下:
advice.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="advice_light">你該多吃點</string> <string name="advice_average">體型很棒哦</string> <string name="advice_heavy">你該節食了</string> </resources>
2、程序邏輯結構
MainActivity.java
package com.example.myapp;
import android.app.Activity;
import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.text.DecimalFormat; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //添加按鈕及響應 Button button = (Button)findViewById(R.id.submit); button.setOnClickListener(calcBMI); } private View.OnClickListener calcBMI = new View.OnClickListener() { @Override public void onClick(View v) { DecimalFormat nf = new DecimalFormat("0.00"); //設置顯示格式
//獲取數值並進行計算 EditText fieldheight = (EditText)findViewById(R.id.height); EditText fieldweight = (EditText)findViewById(R.id.weight); double height = Double.parseDouble(fieldheight.getText().toString())/100; double weight = Double.parseDouble(fieldweight.getText().toString()); double BMI = weight / (height * height); //顯示結果 TextView result = (TextView)findViewById(R.id.result); result.setText("YourBMIis "+nf.format(BMI)); //顯示建議 TextView fieldsuggest = (TextView)findViewById(R.id.suggest); if(BMI>25) { fieldsuggest.setText(R.string.advice_heavy); } else if(BMI<20) { fieldsuggest.setText(R.string.advice_light); } else { fieldsuggest.setText(R.string.advice_average); } } }; }