สร้างตัวนับการแตะ
เชื่อมสถานะเข้ากับปุ่มเพื่ออัปเดตแบบทันที
สร้างตัวนับการแตะ เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Our Goal
Let's build a tiny tap counter: a number on screen and a button that increases it with every tap. 🙂
Start With the State
First declare the state that will change. The counter starts at zero and lives inside the Composable for now.
var count by remember { mutableStateOf(0) }Show the Count
Read the state in a Text so it always displays the current value. Reading it here subscribes the Text to changes.
Text("Count: " + count)Add the Button
A Button needs an onClick action. Inside it we will change the state, which is what makes the UI react.
Button(onClick = { }) {
Text("Tap me")
}Increment on Tap
In onClick, bump the counter. Changing the state tells Compose to recompose anything that reads count.
Button(onClick = { count++ }) {
Text("Tap me")
}Stack Them in a Column
Place the text above the button using a Column, which lays out its children vertically, top to bottom.
Column {
Text("Count: " + count)
Button(onClick = { count++ }) { Text("Tap me") }
}How the Loop Works
Tap fires onClick, count changes, Compose recomposes the Text, and you instantly see the new number. That is the full cycle.
Add a Reset Button
Want a fresh start? Add a second button whose onClick sets count back to zero. Reset is just another state change.
Button(onClick = { count = 0 }) {
Text("Reset")
}React to the Value
Because the UI is a function of state, you can style it from count, like showing a message once it passes ten taps.
if (count > 10) Text("Wow, keep going!")Wrap It in a Function
Put everything in a reusable Composable named TapCounter. Now you can drop your counter anywhere in the app.
@Composable
fun TapCounter() {
var count by remember { mutableStateOf(0) }
}You Built Interactivity
That simple loop of state to UI to event to state is the heart of every interactive Compose screen you will ever write.
Quick Check
What makes the displayed number update after a tap?
Recap
You wired state to a Text and a Button to build a live tap counter, your first truly interactive Compose screen. Great work! 🎉
คำถามที่พบบ่อย
บทเรียน “สร้างตัวนับการแตะ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สร้างตัวนับการแตะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สร้างตัวนับการแตะ”
เชื่อมสถานะเข้ากับปุ่มเพื่ออัปเดตแบบทันที คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “สร้างตัวนับการแตะ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม
ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- remember และ mutableStateOf
- การประกอบใหม่: อะไรเป็นตัวกระตุ้นการวาดใหม่
- สร้างตัวนับการแตะ
- rememberSaveable เมื่อหมุนหน้าจอ