Android Compose 下拉选择框 ExposedDropdownMenu下拉选择
/**
* @author 创建人:蒙石瑞
* @date 创建时间:2024/8/23 10:57
* @Description 创建内容:下拉选择框
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CustomExposedDropdownMenu(
options: List<out DropdownMenuDes>,
labelStr: String,
onSelect: (DropdownMenuDes) -> Unit
) {
var expanded by remember {
mutableStateOf(false)
}
var selectedOptionText by remember { mutableStateOf("") }
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {
expanded = !expanded
},
) {
CustomOutlinedTextField(
value = selectedOptionText,
onValueChange = { selectedOptionText = it },
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(
expanded = expanded
)
},
label = { Text(labelStr) },
singleLine = true,
colors = OutlinedTextFieldDefaults.colors(
focusedBorderColor = Color.Blue,
unfocusedBorderColor = Color.White,
cursorColor = Color.Black,
focusedContainerColor = Color.White,
unfocusedContainerColor = Color.White,
focusedLabelColor = Color.Blue,
disabledLabelColor = Color.Black
),
modifier = Modifier
.width(120.dp)
.height(50.dp)
.menuAnchor(),
readOnly = true,
contentPadding = PaddingValues(start = 5.dp, end = 2.dp),
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier.heightIn(max = 250.dp)
) {
options.forEach { option ->
DropdownMenuItem(text = { Text(option.showName) }, onClick = {
selectedOptionText = option.showName
expanded = false
onSelect(option)
})
}
}
}
}
使用
abstract class DropdownMenuDes(val showName: String, val showId: String)
val tempShow = listOf(
DropdownMenuInfo("one", "xxxx"),
DropdownMenuInfo("2222", "xxxx"),
DropdownMenuInfo("33333", "xxxx"),
DropdownMenuInfo("4444", "xxxx"),
DropdownMenuInfo("5555", "xxxx"),
DropdownMenuInfo("666", "xxxx"),
DropdownMenuInfo("777", "xxxx"),
DropdownMenuInfo("999", "xxxx"),
DropdownMenuInfo("1010", "xxxx")
)
Row {
CustomExposedDropdownMenu(tempShow, "垛位") { item ->
ToastUtil.showToast(item.showName)
}
}