เขียนฟังก์ชัน @Composable
กำหนดฟังก์ชันส่วนติดต่อผู้ใช้ของคุณเองที่นำกลับมาใช้ใหม่ได้
เขียนฟังก์ชัน @Composable เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What You Will Build
In this lesson you will write your own Composable from scratch. By the end, you can describe a piece of UI with a single Kotlin function. 🚀
It Starts With @Composable
A Composable is just a function marked with the @Composable annotation. That tag tells Compose this function emits UI instead of returning a value.
@Composable
fun Greeting() {
Text("Hello!")
}Name It Like a Type
Composables that emit UI use PascalCase names, like a class. So you write Greeting, not greeting, to signal it is a UI building block.
They Return Unit
Your Composable does not return a value. It emits UI by calling other Composables inside it, so its return type is simply Unit.
@Composable
fun Welcome() {
Text("Welcome aboard")
}Pass Data With Parameters
Make a Composable reusable by adding parameters. Pass in a name and the same function can greet anyone you like.
@Composable
fun Greeting(name: String) {
Text("Hello, " + name)
}Default Values Help
Give parameters defaults so callers can skip them. Here name defaults to World, so Greeting() still works on its own.
@Composable
fun Greeting(name: String = "World") {
Text("Hello, " + name)
}Built-in Composables
You rarely start from nothing. Compose ships ready-made pieces like Text, Button, and Image that you call from inside your own function.
Import What You Use
When you call Text, Android Studio adds an import for androidx.compose.material3.Text. Let the IDE auto-import so you never guess the package.
Keep Functions Small
Each Composable should describe one clear thing. A small focused function is easier to read, reuse, and preview than a giant one.
No Return, Just Calls
Inside a Composable you do not build and return a widget. You simply call other Composables top to bottom, and Compose draws them in order.
@Composable
fun Card() {
Text("Title")
Text("Subtitle")
}It Is Still Kotlin
A Composable is normal Kotlin underneath. You can use variables, conditions, and loops inside it just like any other function.
@Composable
fun Status(online: Boolean) {
if (online) Text("Online")
else Text("Offline")
}Quick Check
Time for a quick check on what defines a Composable.
Recap
You can now write a Composable: tag it with @Composable, name it in PascalCase, take parameters, and call other Composables to emit UI. 🎉
คำถามที่พบบ่อย
บทเรียน “เขียนฟังก์ชัน @Composable” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เขียนฟังก์ชัน @Composable” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เขียนฟังก์ชัน @Composable”
กำหนดฟังก์ชันส่วนติดต่อผู้ใช้ของคุณเองที่นำกลับมาใช้ใหม่ได้ คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “เขียนฟังก์ชัน @Composable” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม
ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เขียนฟังก์ชัน @Composable
- พรีวิวแบบสดด้วย @Preview
- เรียกใช้ Composable จาก Composable
- setContent: จุดเริ่มต้นของส่วนติดต่อผู้ใช้