Jetpack Compose 中 BasicTextField 的焦点有多清晰?
Posted
技术标签:
【中文标题】Jetpack Compose 中 BasicTextField 的焦点有多清晰?【英文标题】:How clear focus for BasicTextField in Jetpack Compose? 【发布时间】:2021-07-07 13:10:21 【问题描述】:我有一个 Jetpack Compose (Beta04) BasicTextField(带有装饰框)。如何清除焦点?
我尝试过使用 focusRequester 但这不起作用:
val focusRequester = remember FocusRequester()
// ...
BasicTextField(modifier = Modifier.focusRequester(focusRequester), /* ... */)
// ...
placesFocusRequester.freeFocus()
【问题讨论】:
【参考方案1】:要从当前聚焦的组件中清除焦点,您可以使用FocusManager.clearFocus
方法:
val focusRequester = remember FocusRequester()
val focusManager = LocalFocusManager.current
var value by rememberSaveable mutableStateOf("initial value")
BasicTextField(
value = value,
onValueChange = value = it ,
decorationBox = innerTextField ->
Row(
Modifier
.background(Color.LightGray, RoundedCornerShape(percent = 30))
.padding(16.dp)
.focusRequester(focusRequester)
)
//...
innerTextField()
)
Button(onClick = focusManager.clearFocus() )
Text("Clear focus")
【讨论】:
如果我想清除对原生 backpress 的关注,我该如何实现? @AvinashParasurampuram 使用 BackHandler(true) focusManager.clearFocus()以上是关于Jetpack Compose 中 BasicTextField 的焦点有多清晰?的主要内容,如果未能解决你的问题,请参考以下文章
Android Jetpack Compose学习—— Jetpack compose基础布局
Android Jetpack Compose学习—— Jetpack compose基础布局
Android Jetpack Compose学习—— Jetpack compose入门