简体   繁体   English

如何在 Jetpack Compose 中将焦点从一个组件转移到另一个组件?

[英]How to move focus from one component to another in Jetpack compose?

I am trying to move the focus from one component to another on a button click.我试图通过单击按钮将焦点从一个组件移动到另一个组件。 I have this code right now.我现在有这段代码。

    ...
            Column(
            modifier = Modifier
                .fillMaxSize()
                .statusBarsPadding()
                .navigationBarsPadding()
                .background(surfaceColors.surface)
        ) {
            TopBar(
                TopBarState(
                    endText = if (theViewPages[state.currentPageIndex].isShowSkip) stringResource(id = R.string.Skip) else null,
                    onEndTextPressed = { store.dispatch(TheViewAction.OnSkip) },
                    isBackButtonVisible = false
                ) //need to focus on this component when user clicks on button
            )
    
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
                modifier = Modifier
                    .fillMaxHeight()
            ) {
                HorizontalPager(
                    HorizontalPagerState(
                        modifier = Modifier
                            .aspectRatio(1f / 1.5f),
                        count = theViewPages.size,
                        onPageChange = { store.dispatch(TheViewAction.OnPageChange(it)) },
                        manuallyScrollPage = state.manuallyScrollPage,
                        content = { currentPage ->
                            TheItemView(
                                item = TheViewItemModel(
                                    theViewPages[currentPage].isShowSkip,
                                    theViewPages[currentPage].title,
                                    theViewPages[currentPage].image,
                                    theViewPages[currentPage].description,
                                    theViewPages[currentPage].buttonText
                                ),
                                onButtonClick = {
                                    if (state.currentPageIndex != theViewPages.size - 1) {
                                        //when user clicks this button focus moves to above component
store.dispatch(TheViewAction.ManuallyScrollPage)
                                    } else {
                                        store.dispatch(TheViewAction.OnGettingStarted)
                                    }
                                }
                            )
                        }
                    )
                )
            }
        }
    ...

and I have this TheItemView我有这个TheItemView

@Composable
fun TheItemView(
    item: TheViewItemModel,
    onButtonClick: () -> Unit
) {
    val typoColors = EnhanceTheme.colors.typoColors
    val defaultPadding = dimensionResource(id = DesignSystem.dimen.borderDefault)
    val largePadding = dimensionResource(id = DesignSystem.dimen.large)

    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

...
        Text(
            text = "ABCDF",
            modifier = Modifier.padding(horizontal = largePadding)
        )

        Column(
            verticalArrangement = Arrangement.Bottom,
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.weight(1f)
        ) {
            Button(
                buttonState =
                ButtonState(
                    label = "TEST,
                    onClick = onButtonClick,
                  
                   
                )
            )
        }
            ...
    }
}

I want to do this for accessibility.我想这样做是为了可访问性。 I have tried couple of option online but none of them worked.我在网上尝试了几个选项,但都没有用。 tried this Jetpack Compose: Move focus between TextFields using D-Pad without onKeyEvent already and similar Thanks.尝试了这个Jetpack Compose:在没有 onKeyEvent 的情况下使用方向键在 TextFields 之间移动焦点,类似的谢谢。

You can use FocusRequester您可以使用FocusRequester

Step 1第1步

val scope = rememberCoroutineScope()
val focusRequester = remember { FocusRequester() }

Step 2第2步

Attach FocusRequester to the modifier of target composableFocusRequester附加到目标可组合项的modifier

Modifier.focusRequester(focusRequester)

Step 3步骤 3

Request focus like this inside onClick lambda像这样在里面请求焦点onClick lambda

scope.launch { focusRequester.requestFocus() }

Example例子

Surface(
   modifier = Modifier.padding(16.dp),
   color = MaterialTheme.colorScheme.background
) {
    val scope = rememberCoroutineScope()
    val focusRequester = remember { FocusRequester() }
    var email by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }
    Column(
        modifier = Modifier
            .fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(10.dp)
    ) {
        OutlinedTextField(
            value = email,
            onValueChange = { email = it },
            modifier = Modifier
                .fillMaxWidth(),
            placeholder = {
                Text(text = "email")
            }
        )
        OutlinedTextField(
            value = password,
            onValueChange = { password = it },
            modifier = Modifier
                .fillMaxWidth()
                .focusRequester(focusRequester),
            placeholder = {
                Text(text = "password")
            }
        )
        Button(onClick = {
            scope.launch {
                focusRequester.requestFocus()
            }
        }) {
            Text(text = "Move focus on password field")
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Jetpack Compose 中从一个 NavHost 向上导航到另一个? - How to navigate up from one NavHost to another in Jetpack Compose? 如何在 Jetpack Compose 中将图像从一个可组合 function 传递到另一个? - How to pass an image from one composable function to another in Jetpack Compose? Jetpack Compose - 如何根据另一个组件的高度调整填充? - Jetpack Compose - how to adjust the padding depending on the height of another component? Jetpack Compose 导航组件会使用多少个 ViewModel? - How many ViewModels would one use with Jetpack Compose navigation component? 如何从另一个可组合屏幕为一个可组合屏幕异步安排 API 请求? (喷气背包组成) - How to schedule an API request asynchronously for one composable screen from another composable screen? (Jetpack Compose) 如何在 Jetpack Compose 中将视图模型从一个屏幕共享到另一个屏幕? - How can I share viewmodel from one screen to another screen in jetpack compose? 如何在 Jetpack 的 Compose 中管理 Focus state - How to manage Focus state in Jetpack's Compose Jetpack Compose 中 BasicTextField 的焦点有多清晰? - How clear focus for BasicTextField in Jetpack Compose? 如何在 Jetpack Compose 中围绕 canvas 移动矩形? - How to move a rectangle around a canvas in Jetpack Compose? 如何使用 navController 在 Jetpack Compose 中从一个屏幕导航到另一个屏幕? - How to navigate from a screen to another in Jetpack Compose using navController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM