Android Androidsrc Package Name Package Activity package tw

  • Slides: 45
Download presentation

Android程式的執行流程 Android程式專案的程式碼是放在專案的src資料夾,該資料 夾中會根據建立專案時輸入的Package Name建立一個 Package,再把程式檔置於其中,而程式檔的名稱就是在建 立專案時輸入的Activity名稱。 package tw. android; import android. app. Activity; import

Android程式的執行流程 Android程式專案的程式碼是放在專案的src資料夾,該資料 夾中會根據建立專案時輸入的Package Name建立一個 Package,再把程式檔置於其中,而程式檔的名稱就是在建 立專案時輸入的Activity名稱。 package tw. android; import android. app. Activity; import android. os. Bundle; 指定程式 介面 public class Main extends Activity { /** Called when the activity is first created. */ @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); } } 4

程式專案的資源類別 gen/(Package Name)/R. java,這個類 別中整合了這個程式專 案中所有的資源,包括 drawable(圖檔)、 layout(程式介面)、和 string(字串)等不同的 子類別。它是由Android 程式編譯器自動產生, 請不要做任何修改,其 中的子類別layout中的 main屬性就是對應到專

程式專案的資源類別 gen/(Package Name)/R. java,這個類 別中整合了這個程式專 案中所有的資源,包括 drawable(圖檔)、 layout(程式介面)、和 string(字串)等不同的 子類別。它是由Android 程式編譯器自動產生, 請不要做任何修改,其 中的子類別layout中的 main屬性就是對應到專 案下的 res/layout/main. xml檔。 package tw. android; public final class R { public static final class attr { } public static final class drawable { public static final int icon=0 x 7 f 020000; } public static final class layout { public static final int main=0 x 7 f 030000; } public static final class string { public static final int app_name=0 x 7 f 040001; public static final int hello=0 x 7 f 040000; } 6 }

單元6 使用Text. View、Edit. Text和 Button介面元件 12

單元6 使用Text. View、Edit. Text和 Button介面元件 12

Text. View介面元件 <Text. View android: id="@+id/txt. Result" android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="程式執行結果" />

Text. View介面元件 <Text. View android: id="@+id/txt. Result" android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="程式執行結果" /> 增加一個名為txt. Result的Text. View元件,它的寬度是填滿它 所在的外框。它的高度則由文字的高度來決定,元件中會顯 示「程式執行結果」這個字串。 14

Edit. Text介面元件 <Edit. Text android: id="@+id/edt. Sex" android: layout_width="fill_parent" android: layout_height="wrap_content" android: input. Type="text"

Edit. Text介面元件 <Edit. Text android: id="@+id/edt. Sex" android: layout_width="fill_parent" android: layout_height="wrap_content" android: input. Type="text" android: text="" /> 上面的範例中新出現了一個android: input. Type屬性,它是用 來限制這個元件中可以輸入的字元類型,text表示任何字元 都可以接受,如果設定成number則只能輸入 0~9的數字字元。 17

Button介面元件 <Button android: id="@+id/btn. Do. Sug" android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="執行" /> 會在手機螢幕上建立一個按鈕,按鈕上面顯示「執行」。

Button介面元件 <Button android: id="@+id/btn. Do. Sug" android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="執行" /> 會在手機螢幕上建立一個按鈕,按鈕上面顯示「執行」。 19

介面佈局檔範例 <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android"

介面佈局檔範例 <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent" > <Text. View android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="性別:" /> <Edit. Text android: id="@+id/edt. Sex“ android: layout_width="fill_parent" android: layout_height="wrap_content" android: input. Type="text" android: text="" /> <Text. View android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="年齡:" /> <Edit. Text android: id="@+id/edt. Age" android: layout_width="fill_parent" android: layout_height="wrap_content" android: input. Type="number" android: text="" /> <Button android: id="@+id/btn. Do. Sug" android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="婚姻建議" /> <Text. View android: id="@+id/txt. Result" android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="結果" /> </Linear. Layout> 20

設定Button元件的click事件listener Android這種圖形介面的程式架構就是利用各式各樣的事件 處理程序來執行使用者的操作,而事件處理程序在Android 程式中就稱為事件listener。要設定Button元件的事件listener 需要完成下列 3個步驟: Step 1. 建立一個Button的On. Click. Listener物件 Button. On. Click.

設定Button元件的click事件listener Android這種圖形介面的程式架構就是利用各式各樣的事件 處理程序來執行使用者的操作,而事件處理程序在Android 程式中就稱為事件listener。要設定Button元件的事件listener 需要完成下列 3個步驟: Step 1. 建立一個Button的On. Click. Listener物件 Button. On. Click. Listener btn. Do. Sug. On. Click = new Button. On. Click. Listener() { public void on. Click(View v) { // 按下按鈕後要執行的程式碼 … } } 24

設定Button元件的click事件listener Step 3. 把在第一個步驟中建立的On. Click. Listener物件,設 定成第二個步驟中的Button物件的click事件listener btn. Do. Sug. set. On. Click. Listener(btn.

設定Button元件的click事件listener Step 3. 把在第一個步驟中建立的On. Click. Listener物件,設 定成第二個步驟中的Button物件的click事件listener btn. Do. Sug. set. On. Click. Listener(btn. Do. Sug. On. Click); 26

程式檔的架構 public class Main extends Activity { private Button btn. Do. Sug; private Edit.

程式檔的架構 public class Main extends Activity { private Button btn. Do. Sug; private Edit. Text edt. Sex, edt. Age; private Text. View txt. Result; txt. Result = (Text. View)find. View. By. Id(R. id. txt. Result); // 設定button元件的事件listener btn. Do. Sug. set. On. Click. Listener( btn. Do. Sug. On. Click); /** Called when the activity is first created. */ @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); // 從資源類別R中取得介面元件 btn. Do. Sug = (Button)find. View. By. Id(R. id. btn. Do. Sug); edt. Sex = (Edit. Text)find. View. By. Id(R. id. edt. Sex); edt. Age = (Edit. Text)find. View. By. Id(R. id. edt. Age); } private Button. On. Click. Listener btn. Do. Sug. On. Click = new Button. On. Click. Listener() { public void on. Click(View v) { // 按下按鈕後要執行的程式碼 … } }; } 27

完整的Button元件的On. Click. Listener程式 private else 碼Button. On. Click. Listener btn. Do. Sug. On. Click

完整的Button元件的On. Click. Listener程式 private else 碼Button. On. Click. Listener btn. Do. Sug. On. Click = new Button. On. Click. Listener() { public void on. Click(View v) { // 按下按鈕後要執行的程式碼 String str. Sex = edt. Sex. get. Text(). to. String(); int i. Age = Integer. parse. Int(edt. Age. get. Text(). to. String()); String str. Sug = "結果:"; if (str. Sex. equals("男")) if (i. Age < 28) str. Sug += "不急"; else if (i. Age > 33) str. Sug += "趕快結婚"; else str. Sug += "開始找對象"; if (i. Age < 25) str. Sug += "不急"; else if (i. Age > 30) str. Sug += "趕快結婚"; else str. Sug += "開始找對象"; txt. Result. set. Text(str. Sug); } }; 30