gpt4 book ai didi

Jetpack Compose TextField 中的 android:selectAllOnFocus

转载 作者:行者123 更新时间:2023-12-05 00:18:10 24 4
gpt4 key购买 nike

传统 EditText在 Android 上支持 android:selectAllOnFocus属性,例如,当用户单击 EditText 时会选择其内容.
使用 androidx.compose.material.TextField 时如何实现此行为?在 Jetpack Compose 中?

最佳答案

您可以从 MutableInteractionSource 收集焦点状态并根据它更改选择状态:

var textFieldValue by remember { mutableStateOf(TextFieldValue("Lorem ipsum")) }
val interactionSource = remember { MutableInteractionSource() }
val isFocused by interactionSource.collectIsFocusedAsState()
LaunchedEffect(isFocused) {
textFieldValue = textFieldValue.copy(
selection = if (isFocused) {
TextRange(
start = 0,
end = textFieldValue.text.length
)
} else {
TextRange.Zero,
}
)
}
TextField(
value = textFieldValue,
onValueChange = { textFieldValue = it },
interactionSource = interactionSource,
)

关于Jetpack Compose TextField 中的 android:selectAllOnFocus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69266572/

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