由于某些原因,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的任何地方时都能打开它。
import kotlinx.coroutines.flow.collect
作为 collect 的源,否则您将会得到一个奇怪的异常。显然 Kotlin 中的某个人决定在同一个库中有第二个名为 collect 的方法 XD。 - quealegriamasalegreif (it is PressInteraction.Release) {
进行检查。有很多答案只是使用val isPressed: Boolean by interactionSource.collectIsPressedAsState()
,这会在您轻触屏幕时立即触发,而不需要释放手指。 - user924enabled = false
来接收回调。 - Irwin Nawrocki