如何使 EditText 像 Jetpack Compose 中的微调器一样工作
How to make EditText act like a spinner in Jetpack compose
我正在尝试让 EditText 视图在单击时显示微调器,然后显示选择内容或至少类似于带下划线的微调器但带有前导和尾随图标的东西。
PS:EditText 视图看起来像一个带有提示、图标和所有
的合适的 EditText 视图
请问有什么方法可以在 Compose 中做到这一点吗?
ExposedDropdownMenu
就是你要找的我假设
@OptIn(ExperimentalMaterialApi::class)
fun ExposedSelectionMenu(
title: String,
index: Int,
options: List<String>,
onSelected: (Int) -> Unit
) {
var expanded by remember { mutableStateOf(false) }
var selectedOptionText by remember { mutableStateOf(options[index]) }
ExposedDropdownMenuBox(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
expanded = expanded,
onExpandedChange = {
expanded = !expanded
}
) {
TextField(
modifier = Modifier.fillMaxWidth(),
readOnly = true,
value = selectedOptionText,
onValueChange = { },
label = { Text(title) },
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(
expanded = expanded
)
},
colors = ExposedDropdownMenuDefaults.textFieldColors(
backgroundColor = Color.White,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent,
)
)
ExposedDropdownMenu(
modifier = Modifier.fillMaxWidth(),
expanded = expanded,
onDismissRequest = {
expanded = false
}
) {
options.forEachIndexed { index: Int, selectionOption: String ->
DropdownMenuItem(
modifier = Modifier.fillMaxWidth(),
onClick = {
selectedOptionText = selectionOption
expanded = false
onSelected(index)
}
) {
Text(text = selectionOption)
}
}
}
}
}
我正在尝试让 EditText 视图在单击时显示微调器,然后显示选择内容或至少类似于带下划线的微调器但带有前导和尾随图标的东西。
PS:EditText 视图看起来像一个带有提示、图标和所有
的合适的 EditText 视图请问有什么方法可以在 Compose 中做到这一点吗?
ExposedDropdownMenu
就是你要找的我假设
@OptIn(ExperimentalMaterialApi::class)
fun ExposedSelectionMenu(
title: String,
index: Int,
options: List<String>,
onSelected: (Int) -> Unit
) {
var expanded by remember { mutableStateOf(false) }
var selectedOptionText by remember { mutableStateOf(options[index]) }
ExposedDropdownMenuBox(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
expanded = expanded,
onExpandedChange = {
expanded = !expanded
}
) {
TextField(
modifier = Modifier.fillMaxWidth(),
readOnly = true,
value = selectedOptionText,
onValueChange = { },
label = { Text(title) },
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(
expanded = expanded
)
},
colors = ExposedDropdownMenuDefaults.textFieldColors(
backgroundColor = Color.White,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent,
)
)
ExposedDropdownMenu(
modifier = Modifier.fillMaxWidth(),
expanded = expanded,
onDismissRequest = {
expanded = false
}
) {
options.forEachIndexed { index: Int, selectionOption: String ->
DropdownMenuItem(
modifier = Modifier.fillMaxWidth(),
onClick = {
selectedOptionText = selectionOption
expanded = false
onSelected(index)
}
) {
Text(text = selectionOption)
}
}
}
}
}