04/22 Android教學
Android 專案介紹
建立專案
-
Create New Project
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
Template 選擇 Empty Activity
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
命名為 BMI calculator, 語言選擇 kotlin
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
注意
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Minimum SDK 表示最低兼容版本 選擇的版本如果太新, 則app很有可能無法在其他裝置上運行!!
- 那就開始我們的kotlin吧~~
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
組織方式
通常以Android專案方式瀏覽專案
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
專案結構
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- manifest : 存放Android APP 的主要程式檔
- java : 存放專案主要的kotlin程式碼
- res : 存放專案不是程式碼的資源檔案, 例如:使用者UI(Layout)、圖片、文字等等。
AndroidManifest.xml
每個Android APP 都會有此檔案, 存放APP的基本資訊(icon、label等)、有哪些 Activity、 Service、 需要用到的權限等等。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
MainActivity.kt
寫 UI 的 Android APP 一開始會執行的地方, 一開始只有 onCreate() 方法, 載入對應的 Layout。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
專案程式碼 也就是主要戰場
寫 UI 的 Android APP 一開始會執行的地方,一開始只有 onCreate()
方法,載入對應的 Layout。
lifecycle
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 當Activity準備要產生時,先呼叫onCreate方法。
- Activity產生後(還未出現在手機螢幕上),呼叫onStart方法。
- 當Activity出現手機上後,呼叫onResume方法。
- 當使用者按下返回鍵結束Activity時, 先呼叫onPause方法。
- 當Activity從螢幕上消失時,呼叫onStop方法。
- 最後完全結束Activity之前,呼叫onDestroy方法。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
官方參考activity_main.xml
MainActivity 的介面 Layout 檔案, 可透過圖形化介面編輯器或是直接編輯 XML 方式操作。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 調色盤:可拉取需要的元件到 UI 上。
- 元件樹:顯示元件之間的結構。
- 設計編輯器:分別在設計(左)和藍圖(右)視角編輯 Layout。前者為App在裝置上面的呈現, 後者為編輯界面。
- 屬性:可以修改、查看你選擇的元件的屬性。
- 觀看模式:可切換使用圖形化介面或 XML 編輯等。
BMI 計算器實作
版面配置
切換至 activity_main.xml,並使用 XML 編輯模式。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
參考範例
Layout
LinearLayout
:以縱向或橫向呈現。
RelativeLayout
:以相對位置呈現。
ConstraintLayout
:RelativeLayout 的延伸,透過物件自已本身與其它物件之間的約束來決定它的位置。
margin
android:layout_margin
:本元件離上下左右各元件的外邊距。
android:layout_marginStart
:本元件離開始的位置的外邊距。
android:layout_marginEnd
:本元件離結束位置的外邊距。
android:layout_marginBottom
:本元件離下部元件的外邊距。
android:layout_marginTop
:本元件離上部元件的外邊距。
android:layout_marginLeft
:本元件離左部元件的外邊距。
android:layout_marginRight
:本元件離右部元件的外邊距。

當然也可以自己拉拉看版面~~

id、位置、長寬、字體大小一樣可以從 attribute 裡面直接更改喔~
注意
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
每一個元件的 attribute 欄位裡面都可以填寫 id 名稱, 為了之後寫程式方便請盡量將他們改成直觀一點的名字!!
使用 findViewById
- 在 layout 中新增 button
android:id="@+id/btn
。id 的格式為 @+id/你想取的名字
。
android:text="calculate"
為按鈕上面的字樣。
模擬器呈現:

- 在 MainActivity.kt 中對按鈕進行綁定及操作。
在setContentView(R.layout.activity_main)
下方加入:
新增點擊事件
BMI 計算器在壓下按鈕的那一刻需要達成:
- 抓取使用者輸入的身高體重
- 計算出bmi
- 顯示 (Toast) 計算結果
在 MainActivity.kt 中對按鈕設定OnClickListener,並使用 Toast 顯示訊息, 格式為:
button的id名稱.setOnClickListener{ }
參考範例
抓取textview
裡面輸入的數值並將其轉為浮點數.toFloat()
變數型態:
val
:表示不可以更動的值
var
:可以更動的數值
將計算結果呈現在UI上
Toast.LENGTH_LONG
跳出結果後呈現較長時間
Toast.LENGTH_SHORT
跳出結果後呈現較短時間
成果展示(模擬器)

將 APP 在手機上驅動
- 將手機用傳輸線連線到電腦
廢話
- 打開 設定->關於手機->按下版本號碼(他要你點幾次就點幾次)
- 確認更改為開發者模式之後再一次進入設定->系統->開發人員選項->開啟USB偵錯
- 選擇自己的手機後 再一次執行程式
- 可以結束ㄌ