kotlin jetpack组合中下拉菜单的默认值

mqxuamgl  于 2022-11-16  发布在  Kotlin
关注(0)|答案(1)|浏览(156)

我想在组件呈现后立即为下拉菜单选择默认值。不想将其设置为标签。我想默认选择下拉菜单中的一个选项,然后用户可以根据自己的喜好更改它
我正在执行类似的操作,它确实设置了默认值,但我无法通过从下拉列表中选择来更改它

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)
                            }
                        }
                    }
                }
4c8rllxm

4c8rllxm1#

您可以尝试以下操作:

val options = listOf("Option 1", "Option 2", "Option 3", "Option 4", "Option 5")
var expanded by remember { mutableStateOf(false) }
var selectedOptionText by remember { mutableStateOf(options[1]) }

// We want to react on tap/press on TextField to show menu
ExposedDropdownMenuBox(
    expanded = expanded,
    onExpandedChange = {
        expanded = !expanded
    }
) {
    TextField(
        readOnly = true,
        value = selectedOptionText,
        onValueChange = { },
        label = { Text("Label") },
        trailingIcon = {
            ExposedDropdownMenuDefaults.TrailingIcon(
                expanded = expanded
            )
        },
        colors = ExposedDropdownMenuDefaults.textFieldColors()
    )
    ExposedDropdownMenu(
        expanded = expanded,
        onDismissRequest = {
            expanded = false
        }
    ) {
        options.forEach { selectionOption ->
            DropdownMenuItem(
                onClick = {
                    selectedOptionText = selectionOption
                    expanded = false
                }
            ) {
                Text(text = selectionOption)
            }
        }
    }
}

相关问题