gpt4 book ai didi

android - 使用 Jetpack Compose 在单击按钮时动态添加 View

转载 作者:行者123 更新时间:2023-12-04 23:59:12 25 4
gpt4 key购买 nike

我想在每次用户单击“添加”按钮时将文本字段动态添加到我的布局中。添加的文本字段应添加到“添加”按钮上方。即在步骤 1 文本字段和添加按钮之间。如何通过 Jetpack Compose 实现这一点?下面是屏幕截图后跟我当前的代码..
enter image description here
代码-

Column(modifier = Modifier.padding(16.dp)) {

OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
value = step1,
onValueChange = {
viewModel.onStep1Changed(it)
},
label = {
Text(text = "Step 1...")
},
shape = RoundedCornerShape(8.dp),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent),
trailingIcon = {
Icon(
modifier = Modifier.padding(start=10.dp),
imageVector = Icons.Filled.Image,
tint= Color.Blue,
contentDescription = "Select Image"
)
}
)
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = {
//TODO Dynamically add text fields
}){
Text("Add")
}

}

最佳答案

Column(modifier = Modifier.padding(16.dp)) {
val textFieldCount by remember { mutableStateOf (1) }
repeat(textFieldCount) {
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
value = step1,
onValueChange = {
viewModel.onStep1Changed(it)
},
label = {
Text(text = "Step 1...")
},
shape = RoundedCornerShape(8.dp),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent),
trailingIcon = {
Icon(
modifier = Modifier.padding(start=10.dp),
imageVector = Icons.Filled.Image,
tint= Color.Blue,
contentDescription = "Select Image"
)
}
)
Spacer(modifier = Modifier.height(16.dp))
}
Button(onClick = {
textFieldCount++
}){
Text("Add")
}

}

关于android - 使用 Jetpack Compose 在单击按钮时动态添加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68359145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com