我想在组件呈现后立即为下拉菜单选择默认值。不想将其设置为标签。我想默认选择下拉菜单中的一个选项,然后用户可以根据自己的喜好更改它
我正在执行类似的操作,它确实设置了默认值,但我无法通过从下拉列表中选择来更改它
val inspectorList = searchViewModel.inspectors.collectAsState().value
var defaultSelectedInspector = ""
var selectedInspector by remember { mutableStateOf(defaultSelectedInspector)}
if (inspectorList?.isNotEmpty() == true) {
defaultSelectedInspector = inspectorList[0]
selectedInspector = defaultSelectedInspector
}
这是我的下拉菜单代码
Box(modifier = Modifier.fillMaxWidth()) {
OutlinedTextField(
value = selectedInspector,
onValueChange = {},
enabled = false,
modifier = Modifier
.clickable { showInspectorDropdown = !showInspectorDropdown }
.onGloballyPositioned { coordinates ->
textFieldSize = coordinates.size.toSize()
},
colors = TextFieldDefaults.textFieldColors(
disabledTextColor = Color.DarkGray,
backgroundColor = Color.Transparent
),
trailingIcon = {Icon(imageVector = inspectorDropDownIcon, contentDescription = "")}
)
DropdownMenu(
expanded = showInspectorDropdown,
onDismissRequest = { showInspectorDropdown = false },
Modifier.width(with(LocalDensity.current) { textFieldSize.width.toDp() })
) {
inspectorList?.forEach { inspector ->
DropdownMenuItem(
onClick = {
showInspectorDropdown = false
selectedInspector = inspector
}) {
Text(text = inspector)
}
}
}
}
1条答案
按热度按时间4c8rllxm1#
您可以尝试以下操作: