本文主要是介绍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.showNameexpanded = falseonSelect(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)}}
这篇关于Android Compose 下拉选择框 ExposedDropdownMenu下拉选择的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!