You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Composable点击图片切换无响应问题求助

柠檬水应用图片不切换问题修复

核心问题分析

你的代码存在两个关键问题导致图片无法切换:

  1. 未使用Compose状态管理:普通变量(如currentPosition)的变化不会触发Composable重组,UI自然不会更新。
  2. 逻辑逻辑混乱:阶段判断条件错误,点击事件未正确更新状态和资源。

修复后的完整代码

package com.example.lemonade

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.shape.AbsoluteRoundedCornerShape
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.lemonade.ui.theme.LemonadeTheme

class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            LemonadeTheme {
                LemonadeWithTextAndImage()
            }
        }
    }
}

@Preview
@Composable
fun LemonadeApp() {
    LemonadeWithTextAndImage()
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LemonadeWithTextAndImage(
    modifier: Modifier = Modifier
        .fillMaxSize()
        .background(color = Color.White)
) {
    // 定义四个阶段:1=柠檬树,2=挤柠檬,3=柠檬水,4=空杯子
    var currentStep by remember { mutableStateOf(1) }
    // 挤柠檬需要的随机次数(2-4)
    var squeezeNeeded by remember { mutableStateOf((2..4).random()) }
    // 当前已挤次数
    var currentSqueezes by remember { mutableStateOf(0) }

    // 根据当前阶段获取图片资源
    val imageRes = when(currentStep) {
        1 -> R.drawable.lemon_tree
        2 -> R.drawable.lemon_squeeze
        3 -> R.drawable.lemon_drink
        else -> R.drawable.lemon_restart
    }

    // 根据当前阶段获取提示文本
    val textRes = when(currentStep) {
        1 -> R.string.tap_the_lemon_tree
        2 -> R.string.keep_tapping_to_squeeze
        3 -> R.string.tap_to_drink
        else -> R.string.tap_to_restart
    }

    Column(
        modifier = modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        CenterAlignedTopAppBar(
            title = {
                Text("Lemonade")
            },
            colors = TopAppBarDefaults.smallTopAppBarColors(
                containerColor = Color.Yellow,
                titleContentColor = Color.Black
            )
        )
        Spacer(modifier = Modifier.height(200.dp))
        Image(
            painter = painterResource(id = imageRes),
            contentDescription = stringResource(textRes),
            modifier = Modifier
                .clickable {
                    when(currentStep) {
                        1 -> {
                            // 从树进入挤柠檬阶段,重置挤的次数
                            currentStep = 2
                            currentSqueezes = 0
                            squeezeNeeded = (2..4).random()
                        }
                        2 -> {
                            currentSqueezes++
                            // 达到需要的次数后进入喝的阶段
                            if(currentSqueezes >= squeezeNeeded) {
                                currentStep = 3
                            }
                        }
                        3 -> {
                            // 喝了之后进入重置阶段
                            currentStep = 4
                        }
                        4 -> {
                            // 重置回到树的阶段
                            currentStep = 1
                        }
                    }
                }
                .background(
                    color = Color(R.color.icons_background),
                    shape = AbsoluteRoundedCornerShape(10)
                )
        )
        Spacer(modifier = Modifier.height(16.dp))
        Text(text = stringResource(id = textRes))
    }
}

关键修改说明

  • 状态管理:使用remember和mutableStateOf包装currentStep、squeezeNeeded、currentSqueezes,确保变量变化时触发UI重组。
  • 阶段逻辑重构:将流程拆分为4个清晰的阶段,每个阶段的点击行为明确:
    • 柠檬树阶段:点击一次切换到挤柠檬,同时随机生成需要挤的次数。
    • 挤柠檬阶段:累计点击次数,达到要求后切换到喝柠檬水。
    • 喝柠檬水阶段:点击一次切换到空杯子。
    • 空杯子阶段:点击一次重置回柠檬树。
  • 动态文本:根据当前阶段切换提示文本,提升用户体验。
  • 修正资源引用:点击事件中不再错误地用currentPosition作为资源ID,而是通过状态切换来更新图片。

内容的提问来源于stack exchange,提问作者Fishy_Code

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 18:23:15