gpt4 book ai didi

android - Jetpack 撰写 : TextField clickable does not work

转载 作者:行者123 更新时间:2023-12-04 16:37:28 24 4
gpt4 key购买 nike

由于某种原因 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 上的任意位置时打开

最佳答案

用撰写 1.0.2 它默认工作。需要加行enabled = false例子

@Composable
fun SelectableTextField(
modifier: Modifier = Modifier,
textValue: String,
onClick: () -> Unit
) {
TextField(
value = textValue,
onValueChange = {},
modifier = modifier
.fillMaxWidth()
.clickable { onClick() },
enabled = false
)
}
要消除涟漪效应,请使用此类扩展
inline fun Modifier.noRippleClickable(crossinline onClick: () -> Unit): Modifier =
composed {
clickable(indication = null,
interactionSource = remember { MutableInteractionSource() }) {
onClick()
}
}

关于android - Jetpack 撰写 : TextField clickable does not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67902919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com