最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Jetpack Compose TextField state not updating UI when typing - Stack Overflow

programmeradmin1浏览0评论

I'm working on a Jetpack Compose screen where users enter a title in a TextField, and a button is enabled only when the title is not empty. The issue is that even though the title state updates (as confirmed by Logcat), the UI does not reflect the changes—the text in the TextField is not displayed, and the button remains disabled.

@Composable
fun TitleScreen(
    modifier: Modifier = Modifier,
    navigateToInitial: () -> Unit,
    navigateToHelp: () -> Unit,
    navigateToUpload: (Uri, String) -> Unit,
    fileUri: Uri
) {
    var title by rememberSaveable { mutableStateOf("") }

    Column(
        modifier = modifier
            .fillMaxSize()
            .background(White)
            .verticalScroll(rememberScrollState()),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        TextField(
            value = title,
            onValueChange = {
                title = it
                Log.d("TitleScreen", "New title: $title") // Log shows correct updates
            },
            label = { Text("Title") },
            modifier = Modifier
                .fillMaxWidth()
                .height(56.dp)
                .padding(bottom = 16.dp, start = 16.dp, end = 16.dp)
                .clip(CircleShape),
            colors = TextFieldDefaults.colors(
                unfocusedContainerColor = TextFieldColor,
                focusedContainerColor = TextFieldColor,
                focusedTextColor = DarkBlack,
                unfocusedTextColor = DarkBlack,
                focusedIndicatorColor = Color.Transparent,
                unfocusedIndicatorColor = Color.Transparent
            ),
            maxLines = 1
        )

        Button(
            onClick = { navigateToUpload(fileUri, title) },
            colors = buttonColors(containerColor = Blue, disabledContainerColor = LightGray),
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp, vertical = 24.dp)
                .height(50.dp),
            enabled = title.isNotBlank() // Button remains disabled
        ) {
            Text(text = "Next", fontSize = 25.sp, color = White, fontWeight = FontWeight.Bold)
        }
    }
}

Debugging Attempts:

  1. Checked Logcat – The title state updates correctly.
  2. Replaced rememberSaveable with remember – No effect.
  3. Used BasicTextField instead of TextField – Still no UI update.
  4. Tried displaying title in a Text Composable – The Text updates correctly, but TextField remains blank.
  5. Used LaunchedEffect(title) { Log.d("TitleScreen", title) } – Shows updates, but UI doesn't change.

Why is the TextField not updating with the new value of title, and why does the button remain disabled even though title is updated in the state?

发布评论

评论列表(0)

  1. 暂无评论