[英]Why my Jetpack Compose Text does not recomposed while TextField does
在 Android Jetpack Compose 應用程序中,我嘗試了對文本的可點擊更新。
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { ColorBox() }
}
}
@Composable
fun ColorBox() {
val zero = 0
val full = 255
var r by remember { mutableStateOf(zero) }
var g by remember { mutableStateOf(full) }
var b by remember { mutableStateOf(zero) }
val color = Color(r,g,b)
Box(
modifier = Modifier
.fillMaxSize()
.background(color)
.clickable {
val until = 256
r = Random.nextInt(until)
g = Random.nextInt(until)
b = Random.nextInt(until)
},
contentAlignment = Alignment.Center,
) {
Column {
Text(text = "R:$r, G:$g, B:$b")
TextField(value = "R:$r, G:$g, B:$b", onValueChange = {})
}
}
}
當我點擊屏幕時,背景顏色會改變。 我希望 Text 和 TextField 在顏色 RGB 組件方面發生變化。 但是 Text 沒有改變,而 TextField 按預期更改,如下圖所示。
Text Composable 不運行重組,為什么不運行?
問題是 UI 的版本依賴性,感謝@Halifax。
項目:build.gradle:
buildscript {
ext {
// compose_ui_version = '1.1.1'
compose_ui_version = '1.3.3' // -- (*)
}
}
模塊:build.gradle:
dependencies {
// implementation 'androidx.compose.material:material:1.1.1' // -- (*)
implementation 'androidx.compose.material:material:1.3.1'
}
標記為// -- (*)
的版本組合導致上述問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.