英文:
How can I update the "count" within my Kotlin app
问题
以下是代码的翻译:
@Composable
fun DiceWithButtonAndImage(modifier: Modifier = Modifier) {
var count = 3 // 整数
Column(
modifier = modifier,
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(painter = painterResource(R.drawable.android_robot_svg), contentDescription = "1")
Spacer(modifier = Modifier.height(16.dp))
Text("当前计数:$count")
Row() {
Text("Jackson Perry", fontWeight = FontWeight.Bold)
Text("(CS 492)")
}
Row() {
Button(onClick = { count = when(count) {
1 -> 5
2 -> 1
3 -> 2
4 -> 3
5 -> 4
else -> 0
} }) {
Text(stringResource(R.string.prev))
}
Button(onClick = { count = when(count) {
1 -> 2
2 -> 3
3 -> 4
4 -> 5
5 -> 1
else -> 0
} }) {
Text(stringResource(R.string.next))
}
}
}
}
请注意,这只是代码的翻译部分,不包括问题或其他内容。如果您需要更多帮助,请告诉我。
英文:
My app is relatively simple and consists of primarily 3 things: a count, and two buttons to decrement/increment the count. I am unsure if my method of editing the count is out of scope or if the UI needs to be forced to update. Below are the 50 lines of logic.
@Composable
fun DiceWithButtonAndImage(modifier: Modifier = Modifier) {
var count = 3 // Int
Column(
modifier = modifier,
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(painter = painterResource(R.drawable.android_robot_svg), contentDescription = "1")
Spacer(modifier = Modifier.height(16.dp))
Text("Current count: $count")
Row() {
Text("Jackson Perry", fontWeight = FontWeight.Bold)
Text("(CS 492)")
}
Row() {
Button(onClick = { count = when(count) {
1 -> 5
2 -> 1
3 -> 2
4 -> 3
5 -> 4
else -> 0
} }) {
Text(stringResource(R.string.prev))
}
Button(onClick = { count = when(count) {
1 -> 2
2 -> 3
3 -> 4
4 -> 5
5 -> 1
else -> 0
} }) {
Text(stringResource(R.string.next))
}
}
}
}
The UI is appearing as intended, yet the buttons seem to have no effect. Here is how it appears
答案1
得分: 0
为了使变量的值保持不变,您必须记住
它。为了在更改变量后更新UI,您必须使该变量使用State。所以,您需要两者都做。将
var count = 3
更改为
var count: Int by remember { mutableStateOf(3) }
这样就可以了。使用by
而不是=
使State成为一个可变变量代理,因此您可以直接更改其值,而无需每次使用.value
。
另外,您应该尽量避免使用when
语句来处理所有可能的数学条件。您是一名程序员,应该让计算机为您进行计算!
这个将计数减小的代码:
count = when(count) {
1 -> 5
2 -> 1
3 -> 2
4 -> 3
5 -> 4
else -> 0
}
可以改成:
count--
if (count == 0) count = 5
或者,如果您对取余运算符感到舒适,可以改成:
count = ((count + 3) % 5) + 1
同样,增加计数的代码可以改成:
count++
if (count == 6) count = 1
或者
count = (count % 5) + 1
英文:
To make a variable's change in value stick, you have to remember
it. To cause the UI to update after a variable is changed, you have to make the variable use State. So, you need to do both. Change
var count = 3
to
var count: Int by remember { mutableStateOf(3) }
and it will work. Using by
instead of =
makes the State a variable delegate so you can directly change its value without having to use .value
each time.
By the way, you should avoid ever using when statements for handling every possible math condition. You're a programmer, so make the computer do the calculations for you!
This code that decrements the count:
count = when(count) {
1 -> 5
2 -> 1
3 -> 2
4 -> 3
5 -> 4
else -> 0
}
could be
count--
if (count == 0) count = 5
or, if you're comfortable with the remainder operator it could be:
count = ((count + 3) % 5) + 1
Like wise the incrementing code could be
count++
if (count == 6) count = 1
or
count = (count % 5) + 1
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论