admin管理员组文章数量:1224997
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:
- Checked Logcat – The title state updates correctly.
- Replaced rememberSaveable with remember – No effect.
- Used BasicTextField instead of TextField – Still no UI update.
- Tried displaying title in a Text Composable – The Text updates correctly, but TextField remains blank.
- 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?
本文标签: androidJetpack Compose TextField state not updating UI when typingStack Overflow
版权声明:本文标题:android - Jetpack Compose TextField state not updating UI when typing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739430843a2162795.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论