編輯:關於 Android 程式設計
@author ASCE1885(Github/簡書/微博/CSDN)
Kotlin 是由 JetBrains 設計的開放原始碼語言,在 Java 開發者之間越來越受歡迎,常被視為 Java 的後繼者。相較於 Java,它更現代、更具表達力也更簡潔,能提供截然不同的開發體驗。
如果你正在尋找 Android 開發的替代語言,Kotlin 值得一試。它既能完全取代 Java,也能與既有 Java 程式碼混用——這一點對既有專案的遷移尤其關鍵。
以下說明如何在 Android Studio 專案中使用 Kotlin 與相關插件。
開始之前,請先確認兩件事:
若對 Kotlin 不熟悉,建議先讀 Kotlin 官方指南的「入門」章節。
在 Android Studio 的歡迎畫面選擇 Configure → Plugins:

在開啟的頁面點擊下方的 Install JetBrains plugin…:

在列表中搜尋並選擇 Kotlin Extensions For Android,點擊右側的 Install Plugin:

由於該擴充 depends on Kotlin 本體插件,會跳出提示要求一併下載,點 Yes 開始安裝:

下載安裝完成後重啟 Android Studio,插件即會生效。
在專案包名上按右鍵,選擇 New → Kotlin File:

在對話框中輸入類別名稱並選擇 Class,這裡命名為 MainActivity:

類別建立後會出現一則警告,提示需要為 app 模組加上 Kotlin 支援:

點擊警告條上的連結,在彈出的對話框中按 OK 採用預設版本:

插件會相應修改 build.gradle,點擊 Sync Now 讓變更生效:

至此專案配置完成,可以回到剛才建立的類別開始寫 Kotlin。
為了保持簡單,我們做一個只包含 TextView 的 Activity。
讓類別繼承自 Activity 並覆寫 onCreate。若不熟語法,可用 Control+O 叫出 Android Studio 的程式碼生成功能,讓它給你正確的函式簽名:

package com.hathy.kotlinsample
import android.app.Activity
import android.os.Bundle
public class MainActivity: Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
用 val 建立一個唯讀的 TextView 實例:
val myMessage = TextView(this)
設定文字,再交給 setContentView:
myMessage.setText("Hello")
setContentView(myMessage)
跟 Java 一樣,Kotlin Activity 也必須在 AndroidManifest.xml 中宣告,系統才認得它。
現在就可以在裝置或模擬器上執行了。Kotlin 編譯器比 Java 稍慢一些,但實際感受的差異並不明顯。
這個插件讓你能像操作 Activity 屬性一樣操作 XML 佈局中的控制項——也就是不必再呼叫 findViewById。這些屬性被稱為「合成屬性」(synthetic properties)。
在 app 模組的 build.gradle 中加入建置腳本依賴,別忘了 Sync:
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
接著建立一個用 XML 定義 TextView 的 Activity。佈局檔 another_activity.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/myMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
新建 Kotlin 類別 AnotherActivity:
package com.hathy.kotlinsample
import android.app.Activity
import android.os.Bundle
public class AnotherActivity: Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.another_activity)
}
}
現在用一行 import 取代 findViewById:
import kotlinx.android.synthetic.another_activity.myMessage
若佈局中還有其他控制項,可以一次全部引入:
import kotlinx.android.synthetic.another_activity.*
之後就能像使用類別屬性一樣,直接用 id 存取 TextView:
myMessage.setText("Hello")
插件也支援直接轉換。先寫一個簡單的 Java Activity:
public class YetAnotherActivity extends Activity {
private int a, b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
a = 10;
b = 20;
Log.d("MESSAGE", "The sum is " + (a + b));
}
}
用 Control+Alt+Shift+J,或選單 Code → Convert Java File to Kotlin File:

轉換結果:
public class YetAnotherActivity : Activity() {
private var a: Int = 0
private var b: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
a = 10
b = 20
Log.d("MESSAGE", "The sum is " + (a + b))
}
}
除了內容,副檔名也從 .java 變成 .kt。
本文示範了安裝 Kotlin 與 Kotlin Android Extensions 插件,並在 Android Studio 中實際使用 Kotlin 撰寫 Activity、以合成屬性取代 findViewById、以及把 Java 類別一鍵轉成 Kotlin。
由於 Kotlin 與 Java 在很大程度上可互通,若你還在學習階段,建議逐步替換而非一次全改——先從新檔案開始,等語法熟了再回頭處理舊程式碼。
文末美圖一幅

Android 自訂「載入中」Dialog 實例
開發中經常需要發送網路請求取得資料,而從發出請求到拿到資料之間往往需要等待一段時間。為了提升使用者體驗,我們通常會用一個 Dialog 來提示使用者「資料正在載入」。 今
Android 的 SeekBar 與 RatingBar 使用詳解
SeekBar 簡介 SeekBar 允許用戶拖動以進行調節,常用於音量、亮度等場景。通過 android:thumb 可以設置表示拖動滑塊的 drawable;通過 s
Android Fresco 圖片框架入門:高效載入、快取與顯示優化
Fresco 是 Facebook 推出的圖片載入框架,支援多級快取、漸進式顯示、GIF 與 WebP 等格式,適合需要大量圖片呈現的應用。使用前需在 build.gra
Android 學習筆記:Button 點擊事件切換圖片
(1)在佈局檔中為 Button 設定 selector 背景: android:background=@drawable/button_selector(2)在 r