由于某种原因 Compose TextField 的点击监听器对我不起作用。
@Composable
private fun ExposedDropdown(
modifier: Modifier,
list: List<String>,
priority: Int
) {
var expanded by remember { mutableStateOf(false) }
Column(modifier) {
OutlinedTextField(
value = list[priority],
onValueChange = { },
readOnly = true,
singleLine = true,
label = { Text(stringResource(id = R.string.status)) },
modifier = Modifier
.fillMaxWidth()
.clickable { Timber.i("Not working :(") }
.onFocusChanged { if (it.isFocused) expanded = !expanded },
trailingIcon = {
Icon(
imageVector = Icons.Outlined.ArrowDropDown,
contentDescription = null,
modifier = Modifier
.clickable { expanded = !expanded }
.padding(16.dp)
)
}
)
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
list.forEach { label ->
DropdownMenuItem(onClick = {
viewModel.setPriority(list.indexOf(label))
expanded = false
}) {
Text(text = label)
}
}
}
}
}
如您所见,我提出了不好的解决方案,onFocusChanged
但效果不佳。
对于那些需要上下文的人,我正在尝试做 ExposedDropdown 但我希望它在我单击 TextField 上的任意位置时打开