我尝试禁用textFields来强制用户按一定顺序完成表单。我创建了一个由4个ExposedDropdownMenuBox
和它们的TextField
组成的表单,并添加了一个FocusManager
,以便在选择值时从一个输入移动到Next
。
我想找到一种方法,通过在前面的输入未完成时禁用输入,强制以该顺序做出“选择A -〉... -〉选择D”。例如,我不想允许选择B,因为没有做出选择A。
根据文档,这是我当前的代码:(I also created a gist here)
@Composable
fun TransferDestination() {
val optionsA = listOf("-", "A1", "A2", "A3", "A4")
val optionsB = listOf("-", "B1", "B2", "B3", "B4")
val optionsC = listOf("-", "C1", "C2", "C3", "C4",)
val optionsD = listOf("-", "D1", "D2", "D3", "D4",)
var selectedA by remember { mutableStateOf("-") }
var selectedB by remember { mutableStateOf("-") }
var selectedC by remember { mutableStateOf("-") }
var selectedD by remember { mutableStateOf("-") }
var expanded0 by remember { mutableStateOf(false) }
var expanded1 by remember { mutableStateOf(false) }
var expanded2 by remember { mutableStateOf(false) }
var expanded3 by remember { mutableStateOf(false) }
Column() {
InputSelect(
label = "Choice A",
isEnabled = true,
expanded = expanded0,
list = optionsA,
value = selectedA,
onExpandedChange = {
expanded0 = !expanded0
},
onSelect = {
selectedA = it
if (it != "-") {
focusManager.moveFocus(FocusDirection.Next)
expanded1 = !expanded1
}
}
)
InputSelect(
label = "Choice B",
isEnabled = true,
expanded = expanded1,
list = optionsB,
value = selectedB,
onExpandedChange = {
expanded1 = !expanded1
},
onSelect = {
selectedB = it
if (it != "-") {
focusManager.moveFocus(FocusDirection.Next)
expanded2 = !expanded2
}
}
)
// and same with C and D
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun InputSelect(
label: String = "Label",
isEnabled: Boolean,
expanded: Boolean,
list: List<String>,
value: String,
onExpandedChange: () -> Unit,
onSelect: (str: String) -> Unit
) {
// We want to react on tap/press on TextField to show menu
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {
println("onExpandedChange: label? $label; isEnabled? $isEnabled")
if (isEnabled) {
onExpandedChange()
}
},
) {
OutlinedTextField(
// The `menuAnchor` modifier must be passed to the text field for correctness.
modifier = Modifier
.menuAnchor()
.padding(4.dp),
// enabled = isEnabled,
enabled = isEnabled,
readOnly = true,
value = value,
onValueChange = {},
label = { Text(label) },
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
colors = ExposedDropdownMenuDefaults.textFieldColors(),
shape = RoundedCornerShape(8.dp),
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = {
onExpandedChange()
},
) {
list.forEach { selectionOption ->
DropdownMenuItem(
text = { Text(selectionOption) },
onClick = {
// selectedOptionText = selectionOption
// expanded = false
onSelect(selectionOption)
onExpandedChange()
println("select + change expand")
},
contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding,
)
}
}
}
}
有什么办法可以让我做这个吗?先谢谢你了。
1条答案
按热度按时间d8tt03nd1#
我会建议加上这些标志
然后在select时将其更新为true,并将下一个的isEnabled参数设置为前一个标志。
完整代码: