目錄 -1- 目錄 序 準備篇 一 使用說明... 0-2 二 標示說明... 0-6 三 注意事項... 0-7 第一類 Android 基礎知識 -UI 設計及語法應用 101. 整存整付計算機... 1-2 102. 電費計算機... 1-8 103. 點餐系統... 1-18 104. 計算 BMI 值... 1-23 105. MENU 功能選單... 1-36 106. 畫廊展示... 1-42 107. 驗證 Activity... 1-48 108. 選擇手機型號... 1-57 109. AlertDialog 顯示結果... 1-61 110. 動態密碼顯示... 1-69 第二類資料儲存應用 201. 猜數字遊戲... 2-2 202. 公尺與英呎轉換... 2-9 203. 匯率換算... 2-15 204. SQLite 與 MENU 作業... 2-22 205. 圖檔讀取... 2-34 206. 資料庫讀取... 2-40 207. 密碼鎖... 2-51 208. 我是誰?... 2-57
2-2 TQC+ 行動裝置應用程式設計認證指南解題秘笈 -Android 2 209. 姓名清單... 2-64 210. 簡單記事... 2-69 第三類基礎服務應用 301. 通訊錄搜尋... 3-2 302. 接收 SMS... 3-9 303. MP3 播放器... 3-17 304. 接收簡訊... 3-28 305. 接聽來電... 3-35 306. 查詢地圖... 3-40 307. 判斷發送簡訊狀態... 3-51 308. 判斷地標所屬區域... 3-60 309. 畫布程式... 3-69 310. 程式背景音樂... 3-76
Android 基礎知識 -UI 設計及語法應用 101. 整存整付計算機 102. 電費計算機 103. 點餐系統 104. 計算 BMI 值 105. MENU 功能選單 106. 畫廊展示 107. 驗證 Activity 108. 選擇手機型號 109. AlertDialog 顯示結果 110. 動態密碼顯示
1-2 TQC+ 行動裝置應用程式設計認證指南解題秘笈 -Android 2 101. 整存整付計算機 A. 解題要項 螢幕畫面 Layout 設計 strings.xml 檔字串設定 在 Java 檔裡, 取得使用者輸入資料, 經過運算處理後, 將結果顯示在螢幕上 B. 重點描述依照題目畫面的要求, 在 strings.xml 檔裡加入所要顯示的字串, 並給予每個字串 ID, 其指令格式如 :<string name="id"> 顯示的字串 </string> 此題目是以 LinearLayout 來設計, 螢幕畫面 LinearLayout 的特性是所加入的元件依序由上而下 ( 或由左至右 ) 排列 文字顯示使用 <TextView> 元件, 設定使用者輸入的欄位使用 <EditText> 元件, 按鈕使用 <Button> 元件 1 2 3 4 5 6 7 8 9 strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name"> 整存整付計算機 </string> <string name="title"> 整存整付試算 :</string> <string name="lend"> 本金 (NT$):</string> <string name="rate"> 年利率 (%):</string> <string name="number"> 存款期數 :</string> <string name="btn"> 計算本利和 </string> </resources> 程式說明行數說明 3~8 新增 string 並命名與宣告值 101. 整存整付計算機
第一類 Android 基礎知識 -UI 設計及語法應用 1-3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 main.xml <?xml version="1.0" encoding="utf-8"?> <!-- 在考題程式碼中, 使用 LinearLayout, 並且 android:orientation 屬性設定為 "vertical", 所加入的元件依序由上而下排列 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="fill_parent"> <!-- 加入一個 TextView 元件, 並設定顯示內容 --> <TextView android:text="@string/title" android:layout_height="wrap_content" /> <!-- 加入一個 TextView 元件, 並設定顯示內容 --> <TextView android:text="@string/lend" <!-- 加入一個 EditText 元件, 並設定輸入資料型態為 integer --> <EditText android:id="@+id/input_lend" android:layout_height="wrap_content" android:numeric="integer"/> <!-- 加入一個 TextView 元件, 並設定顯示內容 --> <TextView android:text="@string/rate" <!-- 加入一個 EditText 元件, 並設定輸入資料型態為 integer --> <EditText android:id="@+id/input_rate" android:layout_height="wrap_content" android:numeric="integer"/> <!-- 加入一個 TextView 元件, 並設定顯示內容 --> 101. 整存整付計算機
1-4 TQC+ 行動裝置應用程式設計認證指南解題秘笈 -Android 2 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 <TextView android:text="@string/number" <!-- 加入一個 EditText 元件, 並設定輸入資料型態為 integer --> <EditText android:id="@+id/input_number" android:layout_height="wrap_content" android:numeric="integer"/> <!-- 加入一個 Button 元件, 設定按鈕上的顯示文字, 按鈕的動作需要在 Java 程式中處理, 所以需要為此按鈕設定 ID --> <Button android:text="@string/btn" android:id="@+id/submit" <!-- 加入一個 TextView 元件, 此 TextView 元件的顯示內容需要在 Java 程式中處理, 所以需要為此元件設定 ID --> <TextView android:text="" android:id="@+id/amount" </LinearLayout> 程式說明 行數說明加入一個 TextView 元件, 字串的內容是從 ID 為 title 的 string 物 12~14 件取得,TextView 元件的寬度填滿一整層,TextView 元件的高度與所顯示的內容同高加入一個 TextView 元件, 字串的內容是從 ID 為 lend 的 string 17~19 物件取得,TextView 元件的寬度填滿一整層,TextView 元件的高度與所顯示的內容同高加入一個 EditText 元件, 設定此 EditText 元件的 ID(input_lend), EditText 元件的寬度填滿一整層,EditText 元件的高度與所顯示 22~25 的內容同高, 設定此欄位讓使用者輸入整數資料, 鍵盤會出現數字鍵盤加入一個 TextView 元件, 字串的內容是從 ID 為 rate 的 string 物 28~30 件取得,TextView 元件的寬度填滿一整層,TextView 元件的高 101. 整存整付計算機
第一類 Android 基礎知識 -UI 設計及語法應用 1-5 度與所顯示的內容同高 33~36 39~41 44~47 51~54 57~60 加入一個 EditText 元件, 設定此 EditText 元件的 ID(input_rate), EditText 元件的寬度填滿一整層,EditText 元件的高度與所顯示的內容同高, 設定此欄位讓使用者輸入整數資料, 鍵盤會出現數字鍵盤加入一個 TextView 元件, 字串的內容是從 ID 為 number 的 string 物件取得,TextView 元件的寬度填滿一整層,TextView 元件的高度與所顯示的內容同高加入一個 EditText 元件, 設定此 EditText 元件的 ID(input_number), EditText 元件的寬度填滿一整層,EditText 元件的高度與所顯示的內容同高, 設定此欄位讓使用者輸入整數資料, 鍵盤會出現數字鍵盤加入一個 Button 元件, 按鈕上顯示的文字從 ID 為 btn 的 string 物件取得, 設定此元件的 ID(submit),Button 元件的寬度填滿一整層,Button 元件的高度與所顯示的內容同高加入一個 TextView 元件, 字串的內容空白, 設定此元件的 ID(amount),TextView 元件的寬度填滿一整層,TextView 元件的高度與所顯示的內容同高 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 GDD01.java package COM.TQC.GDD01; import java.text.decimalformat; import android.app.activity; import android.os.bundle; /* 以下這些套件所含的類別會在程式中使用到, 所以必須 import 進來 在 Eclipse 中, 可以使用 ctrl+shift+o, 將程式中所用到的類別 import 進來, 程式設計師不用自行編寫 import 的指令 */ import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class GDD01 extends Activity { /* Called when the activity is first created. */ 101. 整存整付計算機
1-6 TQC+ 行動裝置應用程式設計認證指南解題秘笈 -Android 2 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); findviews(); // 將 main 檔裡的元件與 Java 檔裡的物件連結起來 setlisteners(); // 監聽 Button 按鈕被按, 執行相對應的動作 /* 先做物件宣告, 在 main.xml 檔裡, 有 1 個 <Button>,3 個 <EditText>, 還有 1 個有 ID 的 <TextView>*/ private Button button_calc; private EditText field_lend; private EditText field_rate; private EditText field_number; private TextView view_result; /* 透過 findviewbyid 建構子, 建立 main.xml 檔裡的元件與 Java 檔裡的物件連結關係 */ private void findviews(){ button_calc= (Button)findViewById(R.id.submit); field_lend=(edittext)findviewbyid(r.id.input_lend); field_rate=(edittext)findviewbyid(r.id.input_rate); field_number=(edittext)findviewbyid(r.id.input_number); view_result=(textview)findviewbyid(r.id.amount); /* 監聽 button_calc 這個按鈕物件是否被按, 如果被按, 執行 calcmoney 方法 */ private void setlisteners(){ button_calc.setonclicklistener(calcmoney); //calcmoney 方法的宣告及程式碼 private Button.OnClickListener calcmoney=new Button.OnClickListener(){ public void onclick(view v){ // 設定顯示數字的格式, 小數點後 0 位 DecimalFormat nf=new DecimalFormat("0"); // 取得 field_lend 物件裡的資料, 存放在 double 型態的變數 lend 裡 double lend=double.parsedouble(field_lend.gettext().tostring()); // 取得 field_rate 物件裡的資料, 存放在 double 型態的變數 rate 裡 101. 整存整付計算機
第一類 Android 基礎知識 -UI 設計及語法應用 1-7 57 58 59 60 61 62 63 64 65 66 double rate=double.parsedouble(field_rate.gettext().tostring()); // 取得 field_ number 物件裡的資料, 存放在 double 型態的變數 number 裡 double number =Double.parseDouble(field_number.getText().toString()); // 本利和 = 本金 *(1+ 年利率 /12/)%^ 存款期數 double total=lend*math.pow(1+rate/12/100, number); // 設定 view_result 物件的顯示字串 view_result.settext(" 本利和為 "+nf.format(total)); ; 程式說明 行 數說 明 11~14 程式中所需要使用到的類別 20~25 覆寫 oncreate 方法, 加入畫面顯示 選取畫面元件資料, 以及按鍵監聽 處理功能 先物件宣告, 在 main.xml 檔裡, 有 1 個 <Button>, 3 個 29~33 <EditText>, 還有 1 個有 ID 的 <TextView>, 以上這些元件的資料和動作, 都需要在 Java 檔裡處理, 所以 Java 檔裡需要有相對 應的元件 36~42 透過 findviewbyid 建構子, 建立 main.xml 檔裡的元件與 Java 檔裡的物件連結關係 45~47 監聽 button_calc 這個按鈕物件是否被按, 如果被按, 執行 calcmoney 方法 當 button_calc 按鈕被按下時, 執行以下所有的程式 從使用者 50~65 輸入的欄位裡, 將本金 利率 期數取出, 計算出本利和, 再將 結果顯示在 TextView 元件裡 101. 整存整付計算機