我想基于布尔变量禁用ExposedDropdownMenuBox
我的代码看起来像这样:
@Composable
private fun Title(
title: String,
onTitleChange: (String) -> Unit,
isTitleEnabled: Boolean
) {
val options = stringArrayResource(id = R.array.name_titles)
var expanded by remember { mutableStateOf(false) }
var selectedOptionText by remember { mutableStateOf(title) }
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {
expanded = !expanded
}
) {
TextField(
enabled = isTitleEnabled,
modifier = Modifier
.fillMaxWidth()
.greyBordered(),
readOnly = true,
value = selectedOptionText,
onValueChange = { },
label = { Text(stringResource(id = R.string.input_hint_title)) },
trailingIcon = { TrailingIcon(expanded = expanded) },
colors = ExposedDropdownMenuDefaults.outlinedTextFieldColors(
textColor = navyBlue,
focusedLabelColor = nightGray,
backgroundColor = white,
cursorColor = navyBlue,
trailingIconColor = nightGray,
focusedTrailingIconColor = nightGray,
focusedBorderColor = Color.Transparent,
unfocusedBorderColor = Color.Transparent,
disabledBorderColor = Color.Transparent,
)
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
) {
options.forEach { selectionOption ->
DropdownMenuItem(
onClick = {
selectedOptionText = selectionOption
onTitleChange(selectionOption)
expanded = false
}
) {
Text(text = selectionOption)
}
}
}
}
}
因此,我有一个包含ExposedDropdownMenuBox的组合函数,其中包含textField和ExposedDropdownMenu,我想要实现的是,只要isTitleEnabled
变量为true,就能够启用ExposedDropdownMenuBox,而只要isTitleEnabled
变量为false,就不能展开ExposedDropdownMenuBox。
1条答案
按热度按时间n53p2ov01#
您可以在
onExpandedChange
中使用条件来避免展开ExposedDropdownMenuBox
。当用户单击ExposedDropdownMenuBox
时调用此回调。在这种情况下,您也可以删除trailingIcon
。类似于: