Ander Liu
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    2
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Android Studio - Getting Started === * [name=Ander Liu] ###### tags: `Android Studio` [TOC] ## 前情提要 * [Colde Garage Mobile APP](/ngnZwY3yRgirAnWF4G_3mw) * [Java基礎](/x7PCkXtOR9yCMaI4DoRF1w) ## Getting Started * [Getting Started | Android Developer](https://developer.android.com/guide/) ## What is Android Studio * Android Studio 是一個為 Android 平台開發程式的 IDE(整合式開發環境) * 由 Google 和 JetBrains 共同開發 * 基於 JetBrains IntelliJ IDEA,為 Android 開發特殊客製化 ## Installation 1. 下載 [Android Studio](https://developer.android.com/studio/) 並安裝 2. 如果安裝上有任何問題可以來問我 ### Reference * [Install Android Studio | Android Developers](https://developer.android.com/guide/) ## Create an Android project ### 創一個新專案 1. 啟動 Android Studio 2. 點擊 **Start a new Android Studio Project**![](https://developer.android.com/training/basics/firstapp/images/studio-welcome_2x.png) 3. * 在 **Application name** 輸入 "My first App" * 在 **Company domain** 輸入 "`example.com`"! * **Project location** 自己挑一個喜歡的地方吧 * 至於 **Include C++ support** 和 **Include Kotlin support** 皆不用勾選 * 都設定完後,點選 **Next** ![](https://i.imgur.com/2xoRrHj.png) 4. 在 **Target Android Devices** 視窗,保持預設值不變,繼續 **Next** 5. 在 **Add an Activity to Mobile** 視窗,選擇 **Empty Activity**,再 **Next** 6. 在 **Configure Activity** 視窗,保持預設值不變,按下 **Finish** !!! 7. 此時你應該會看到這個畫面![](https://developer.android.com/training/basics/firstapp/images/studio-editor_2x.png) :::success 大功告成~~ ::: ### 介紹重要檔案 左上角有個直的 **Project** 按鈕,點下去便會開啟 **Project** 視窗,在此可以選擇檔案 整個 Android Project 中有兩種最重要的檔案, Java 與 XML * app > java > com.example.myfirstapp > MainActivity > 第一種為 Java,這個 project 的 main Activity,當你執行 APP,系統會啟動這個 [`Activity`](https://developer.android.com/reference/android/app/Activity) 並載入它的 layout * app > res > layout > activity_main.xml > 第二種則為 XML,XML檔定義這個 Activity UI(使用者介面) 的 layout :::warning 有關 [`Activity`](https://developer.android.com/reference/android/app/Activity) 我們之後會再詳細介紹,這裡只要知道他是一個視窗就好 ::: ### Reference * [Create an Android project | Android Developers](https://developer.android.com/training/basics/firstapp/creating-project) ## Run your app * 你可以在自己的手機或是在模擬器上跑這個 APP ### Run on a real device * 你必須先開啟開發人員選項裡的USB偵錯功能,如果不知道如何開啟請 Google 1. 點擊右上角的 **Run**![](https://developer.android.com/studio/images/buttons/toolbar-run.png) 2. 在 **Select Deployment Target** 視窗,選取你的裝置,再按 OK![](https://developer.android.com/training/basics/firstapp/images/run-device_2x.png) ### Run on an emulator 1. 點擊右上角的 **Run**![](https://developer.android.com/studio/images/buttons/toolbar-run.png) 2. 在 **Select Deployment Target** 視窗,選取 **Create New Virtual Device**![](https://developer.android.com/training/basics/firstapp/images/run-avd_2x.png) 4. 在 **Select Hardware** 視窗,任意選一個裝置,例如 Pixel,再點 **Next** 5. 在 **System Image** 視窗,選取最高 API level 的版本,如果沒有安裝過,要先下載。最後再按 **Next** 6. 在 **Android Virtual Device (AVD)** 視窗,保持預設值不變,按下 **Finish** 7. 回到 **Select Deployment Target** 視窗,選取你剛剛創的裝置,按下 **OK** :::success 執行成功~~~ ::: ![](https://i.imgur.com/IfyICKZ.png =270x480) ### Reference * [Run your app | Android Developers](https://developer.android.com/training/basics/firstapp/running-app) ## Build a simple UI ### 介紹元件 * [`TextView`](https://developer.android.com/reference/android/widget/TextView) > 普通的文字方塊 * [`EditView`](https://developer.android.com/reference/android/widget/EditText) > 可以編輯的文字方塊,你可以讀取裡面輸入了些什麼 * [`Button`](https://developer.android.com/reference/android/widget/Button) > 就是按鈕,APP 可以在你按下時,執行其他程式 * [`AlertDialog`](https://developer.android.com/reference/android/app/AlertDialog) > 如下圖這種警告視窗,這種物件 Android 已經幫我們寫好了,我們可以直接拿來用 > ![](https://developer.android.com/images/ui/dialogs.png?hl=zh-tw) ### 來做個會跟自己打招呼的APP吧!!! #### Layout 1. 先切換到 **activity_main.xml** 2. 先把預設的 [`TextView`](https://developer.android.com/reference/android/widget/TextView)-"Hello world!" 刪掉 ![](https://i.imgur.com/eWxzUaX.png) 3. 再從 **Palette** 找到 [`TextView`](https://developer.android.com/reference/android/widget/TextView), [`Button`](https://developer.android.com/reference/android/widget/Button), Plain Text,並分別把他們拉進去 layout ![](https://i.imgur.com/DaEIqj8.png) ![](https://i.imgur.com/eJNojuD.png) 4. 再來要介紹 Constraint,有三種對齊方式 * Constraint anchors > 選取該物件,在四邊有圓點可以選取,可以往外拖動連接其他物件或邊界 > ![](https://developer.android.com/training/basics/firstapp/images/constraint-textbox_2x.png) * Baseline > 選取該物件,點擊 **Edit Baseline** ![](https://developer.android.com/studio/images/buttons/layout-editor-action-baseline.png),拖動物件中的 baseline 連接到另一個物件的 baseline,兩個物件便會水平對齊 > ![](https://developer.android.com/training/basics/firstapp/images/constraint-button_2x.png) * Chain > 同時選取兩個物件,按下右鍵選擇 **Chain > Create Horizontal Chain** or **Vertical Chain**![](https://developer.android.com/training/basics/firstapp/images/create_horizontal_chain_2x.png) 5. 自行利用上述方法設計 layout,以下為範例 ![](https://i.imgur.com/HiiAYhq.png) #### JAVA 1. 再切換到 **MainActivity.java** 2. 在code裡面加入 `sendMessage()`method,如下 ```java= public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendMessage(View view){ } } ``` :::danger 你會發現有error, Cannot resolve symbol 'View',此時將滑鼠移到 View 上方,同時按下 Alt + Enter,系統便會自己 import View這個Class ::: 3. 切回 **activity_main.xml**,選取 button 4. 在 **Attributes** 視窗的 **onClick** 屬性選擇 **sendMessage [MainActivity]** 5. 回到 **MainActivity.java**,加入 `sendMessage()` code,使APP讀取我們輸入的名字,並顯示在 [`TextView`](https://developer.android.com/reference/android/widget/TextView)上,如下 ```java= public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendMessage(View view){ EditText editText = findViewById(R.id.editText); TextView textView = findViewById(R.id.textView); String message = editText.getText().toString(); textView.setText(message); } } ``` :::warning code講解 * [`findViewById()`](https://developer.android.com/reference/android/app/Activity#findViewById(int))為 Class [`Activity`](https://developer.android.com/reference/android/app/Activity) 內建的method,他可以透過id來取得這個物件,我們必須透過這個方式取得layout裡的物件,而id為`R.id.[物件名稱]` * [`getText()`](https://developer.android.com/reference/android/widget/EditText#getText())為 [`EditView`](https://developer.android.com/reference/android/widget/EditText)的 method,可以取得你輸入的文字 * 但由於 [`getText()`](https://developer.android.com/reference/android/widget/EditText#getText())的回傳值不為 String,所以我們要使用 [`toString()`](https://developer.android.com/reference/java/util/Objects#toString(java.lang.Object)),java [`Object`](https://developer.android.com/reference/java/util/Objects)內建的method,來轉換成 String * [`setText()`](https://developer.android.com/reference/android/widget/TextView#setText(java.lang.CharSequence))為 [`TextView`](https://developer.android.com/reference/android/widget/TextView)的 method,可以把字串參數顯示在 [`TextView`](https://developer.android.com/reference/android/widget/TextView)上 ::: 6. 再實作 [`AlertDialog`](https://developer.android.com/reference/android/app/AlertDialog)的部分,如果名字輸入為空白即跳出警告視窗 ```java= public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendMessage(View view){ EditText editText = findViewById(R.id.editText); TextView textView = findViewById(R.id.textView); String message = editText.getText().toString(); if(message.isEmpty()) { textView.setText(""); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("Please enter your name!!!"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); AlertDialog dialog = builder.create(); dialog.show(); } else { textView.setText("Hello, " + message + "!!!"); } } } ``` :::warning code講解 * [`isEmpty()`](https://developer.android.com/reference/java/lang/String?hl=zh-tw#isEmpty())為 [`String`](https://developer.android.com/reference/java/lang/String?hl=zh-tw)的 method,如果String == "",便會回傳 1 * [`AlertDialog`](https://developer.android.com/reference/android/app/AlertDialog)必須透過 [`AlertDialog.Builder`](https://developer.android.com/reference/android/app/AlertDialog.Builder?hl=zh-tw)建立內容和版面配置,至於他的constructor參數要丟 [`Activity`](https://developer.android.com/reference/android/app/Activity),所以要打 MainActivity.this * [`setMessage()`](https://developer.android.com/reference/android/app/AlertDialog.Builder?hl=zh-tw#setMessage(java.lang.CharSequence))可以設定 [`AlertDialog`](https://developer.android.com/reference/android/app/AlertDialog)要顯示的訊息,參數輸入要顯示的字串即可 * [`setPositiveButton()`](https://developer.android.com/reference/android/app/AlertDialog.Builder?hl=zh-tw#setPositiveButton(java.lang.CharSequence,%20android.content.DialogInterface.OnClickListener))可以設定確認按鈕,第一個參數為要顯示在按鈕上的字串,第二個參數為 Listener,有關 Listener 之後會再提到 * 編輯完後,就可以創一個新的 [`AlertDialog`](https://developer.android.com/reference/android/app/AlertDialog),呼叫 [`create()`](https://developer.android.com/reference/android/app/AlertDialog.Builder?hl=zh-tw#create())即可 * 最後呼叫 [`show()`](https://developer.android.com/reference/android/app/Dialog?hl=zh-tw#show()),讓 [`AlertDialog`](https://developer.android.com/reference/android/app/AlertDialog)顯示出來 ::: 7. Run執行!!! :::success 成功寫出第一個APP啦~~~ ::: ![](https://i.imgur.com/hozobcq.png =270x480) ![](https://i.imgur.com/EZU5lqy.png =270x480) ### Reference * [Build a simple user interface | Android Developers](https://developer.android.com/training/basics/firstapp/building-ui) * [Start another activity | Android Developers](https://developer.android.com/training/basics/firstapp/starting-activity#java) * [Dialogs | Android Developers](https://developer.android.com/guide/topics/ui/dialogs?hl=zh-tw) ## Demo :::info * 10/29-11/2那週,會再統計大家哪天可以 * 10/27 20:00-22:00 is office hour,其他時間也可以來找我問問題,先跟我說一聲即可 ::: :::danger * 請demo上面那個打招呼APP ::: ## Homepage * [Homepage](/@NDR/Homepage) *[IDE]: Integrated Development Environment *[XML]: Extensible Markup Language *[UI]: User Interface

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully