gpt4 book ai didi

android-jetpack-compose - 在 Jetpack Compose 中手动移动辅助功能焦点

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

使用 Android View,我可以像这样将焦点移动到 View:

fun View.requestAccessibilityFocus() {
requestFocus()
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)
}

我如何在 Jetpack Compose 中实现这一点?

我尝试使用 FocusRequester,但它似乎没有做任何事情:

val lifecycleOwner = LocalLifecycleOwner.current
val requester = FocusRequester()

Box {
...
Image(
...
contentDescription = "My heading",
modifier = Modifier
...
.focusRequester(requester)
)
}

DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_RESUME) {
requester.requestFocus()
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose { lifecycleOwner.lifecycle.removeObserver(observer) }
}

最佳答案

我必须添加 .focusable() 并且必须在 .focusRequester(focusRequester) 之后添加。

Box {
...
Image(
...
contentDescription = "My heading",
modifier = Modifier
...
.focusRequester(requester)
.focusable()
)
}

关于android-jetpack-compose - 在 Jetpack Compose 中手动移动辅助功能焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74187439/

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