0Pricing
Jetpack Compose Academy · 课时

启用、禁用与加载状态

在按钮中反映可用性。

启用、禁用与加载状态 是 CoddyKit 上的免费 Jetpack Compose Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Jetpack Compose Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Jetpack Compose Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Buttons Have Moods

A good button reflects reality: ready, blocked, or working. Compose lets you express these states so users always know what is possible. 🚦

The enabled Parameter

Every button takes an enabled flag. Set it to false and the button greys out and ignores taps automatically.

Button(
    onClick = { submit() },
    enabled = false
) { Text("Submit") }

Drive enabled From State

The real power is binding enabled to state. Tie it to validation so the button only activates when input is ready.

Button(
    onClick = { submit() },
    enabled = name.isNotBlank()
) { Text("Submit") }

Disabled Means Blocked

A disabled button still appears, so users see the action exists. Its faded look signals why not yet without an error message.

Show Progress While Working

For slow actions, show a loading state. Track a boolean and swap the label for a spinner while the work runs.

var loading by remember { mutableStateOf(false) }

Disable During Loading

While loading, set enabled = !loading so users cannot double-tap and fire the same action twice.

Button(
    onClick = { loading = true },
    enabled = !loading
) { Text("Save") }

Swap In a Spinner

Inside the content, branch on the flag: show a small CircularProgressIndicator when loading, the label otherwise.

Button(onClick = { }, enabled = !loading) {
    if (loading) CircularProgressIndicator(Modifier.size(18.dp))
    else Text("Save")
}

Size the Spinner Down

The default spinner is large. Constrain it with a Modifier.size so it fits neatly where the label used to sit.

CircularProgressIndicator(
    modifier = Modifier.size(18.dp),
    strokeWidth = 2.dp
)

Reset When Done

When the work finishes, set loading back to false. The label returns and the button becomes tappable again.

onResult = { loading = false }

States Prevent Mistakes

Together, enabled and loading guard your actions: no taps before input is valid, no duplicate taps while work is in flight.

Communicate, Don't Hide

Prefer disabling over hiding a button. A visible-but-disabled control teaches users the path to unlock it.

Quick Check

Your save action takes two seconds and users keep double-tapping it.

Recap: Stateful Buttons

You learned to reflect reality: enabled gates by validity, a loading flag shows progress and blocks repeats. Honest buttons, happy users. 🎯

常见问题解答

「启用、禁用与加载状态」课时是免费的吗?

是的 — 「启用、禁用与加载状态」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Jetpack Compose Academy 课程的其余内容,请升级到 CoddyKit PRO。 Jetpack Compose Academy 课程共包含 4 节课。

「启用、禁用与加载状态」这节课中我会学到什么?

在按钮中反映可用性。 你通过在浏览器中直接运行的动手代码来练习 Jetpack Compose Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Jetpack Compose Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Jetpack Compose Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「启用、禁用与加载状态」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Jetpack Compose Academy 课中编写并运行代码吗?

能。每节 Jetpack Compose Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Button 与 onClick
  2. 文本、轮廓与图标按钮
  3. 启用、禁用与加载状态
  4. 让任意 Composable 支持 clickable
← 返回 Jetpack Compose Academy