android - Jetpack compose 的公开下拉菜单

标签 android kotlin android-jetpack-compose android-jetpack-compose-text android-compose-textfield

我想知道是否有针对 jetpack compose 的 Exposed 下拉菜单的解决方案?
我在 jetpack compose 中找不到该组件的合适解决方案。有什么帮助吗?
Drop-down

最佳答案

版本 1.1.0-alpha06 介绍了ExposedDropdownMenu的实现基于 ExposedDropdownMenuBox TextFieldDropdownMenu里面。
就像是:

    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[0]) }
    
    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)
                }
            }
        }
    }
enter image description here
使用版本 1.0.x没有内置组件。
您可以使用 OutlinedTextField + DropdownMenu .
这只是一个基本的(非常基本的)实现:
var expanded by remember { mutableStateOf(false) }
val suggestions = listOf("Item1","Item2","Item3")
var selectedText by remember { mutableStateOf("") }

var textfieldSize by remember { mutableStateOf(Size.Zero)}

val icon = if (expanded)
    Icons.Filled.ArrowDropUp //it requires androidx.compose.material:material-icons-extended
else
    Icons.Filled.ArrowDropDown


Column() {
    OutlinedTextField(
        value = selectedText,
        onValueChange = { selectedText = it },
        modifier = Modifier
            .fillMaxWidth()
            .onGloballyPositioned { coordinates ->
                //This value is used to assign to the DropDown the same width
                textfieldSize = coordinates.size.toSize()
            },
        label = {Text("Label")},
        trailingIcon = {
            Icon(icon,"contentDescription",
                 Modifier.clickable { expanded = !expanded })
        }
    )
    DropdownMenu(
        expanded = expanded,
        onDismissRequest = { expanded = false },
        modifier = Modifier
            .width(with(LocalDensity.current){textfieldSize.width.toDp()})
    ) {
        suggestions.forEach { label ->
            DropdownMenuItem(onClick = {
                selectedText = label
            }) {
                Text(text = label)
            }
        }
    }
}
enter image description here
enter image description here

关于android - Jetpack compose 的公开下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67111020/

相关文章:

Android PDFDocument 正在生成一个空白文档

java - 禁用 mongodb 健康指示器 Micronaut

android - Jetpack Compose wrapContentHeight/heightIn 在按钮周围添加了额外的空间

android - Jetpack Compose 中一个 ModalBottomSheetLayout 的多个 BottomSheet

android - 仅在重新启动APP时alertDialogBu​​ilder才会崩溃

android - 如何使用 evothings phonegap 插件从 Polar 设备读取心率值

kotlin - 如何使用 Kotlin channel 实现自然(又名智能)批处理?

android - Android Jetpack Compose 中的图标和图像有什么区别?

android - 华为广告 : check hms sdk available error

android - 类型不匹配 : inferred type is Fragment but YouTubePlayerSupportFragment was expected