タップカウンターを作る
状態をボタンにつなぎ、リアルタイムに更新します。
「タップカウンターを作る」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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! 🎉
よくある質問
「タップカウンターを作る」レッスンは無料ですか?
はい。「タップカウンターを作る」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「タップカウンターを作る」で何を学びますか?
状態をボタンにつなぎ、リアルタイムに更新します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「タップカウンターを作る」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。