简体   繁体   English

在 jetpack compose 中启用/禁用 TextField 后如何管理 TextField 焦点?

[英]How to manage TextField focus after enable/disable the TextField in jetpack compose?

I am working with TextField, and I have faced some issues regarding focus.我正在使用 TextField,并且遇到了一些关于焦点的问题。

Disabled all TextField, only the first one is enabled.禁用所有TextField,仅启用第一个。 When trying to enable other TextField, it lost focus, how we can get back focus?当尝试启用其他 TextField 时,它失去了焦点,我们如何才能重新获得焦点? Screenshot_20220602_121247 Here is my layout:- Screenshot_20220602_121247 这是我的布局:- 在此处输入图像描述

How to shift focus programmatically when enabling TextField?启用 TextField 时如何以编程方式转移焦点?

Here is my code snippet which I have used to enable and disable the TextField.这是我用来启用和禁用 TextField 的代码片段。

fun enabledDisabledTextField(
enteredValues: MutableList<String>,
index: Int,
isEnabled: Boolean): Boolean {
var enabled = isEnabled

val isFirstTime = enteredValues[0].isEmpty()

if (isFirstTime) {
    if (enteredValues[index].isEmpty()) {
        if (index == 0) {
            enabled = true
        }

        if (index > 0) {
            enabled = false
        }
    }
} else {
    if (enteredValues[index].isEmpty()) {
        enabled = true
    }
    if (enteredValues[index].isNotEmpty()) {
        enabled = false
    }
    if (index == enteredValues.size - 1) {
        enabled = true
    }
}
return enabled

} }

I haven't tried to experiment this code yet when dealing with focus management, but there's a function exposed by Modifier where you can manage some focus properties, though I'm not quite sure if this would work and if you need to explicitly supply a FocusRequster object on each of the Box 's child在处理焦点管理时,我还没有尝试过这段代码,但是Modifier公开了一个函数,您可以在其中管理一些焦点属性,尽管我不太确定这是否可行,以及是否需要显式提供每个Box的子对象上的FocusRequster对象

 val focusRequester = remember { FocusRequester.Default }

    Box(modifier = Modifier
        .focusProperties {
            // you might be able to switch focus here
            this.next.requestFocus()
        }
    ) {

        // your text field 1
        TextField(
            modifier = Modifier
                .focusRequester(focusRequester)
        )

        // your text field 2
        TextField(
            modifier = Modifier
                .focusRequester(focusRequester)
        )

        // your text field 3
        TextField(
            modifier = Modifier
                .focusRequester(focusRequester)
        )
    }

FocusManager also provides some convenience with its moveFocus function FocusManagermoveFocus功能也提供了一些便利

val focusManager = LocalFocusManager.current
focusManager.moveFocus(FocusDirection.Next)
    //or
focusManager.moveFocus(FocusDirection.Right)

Guessing your code/logic, call a focusRequester supplied on a textfield's Modifier , again I'm not sure if you have to create a list of FocusRequester and each of them will be supplied to each of your TextField猜测您的代码/逻辑,调用 textfield 的Modifier上提供的focusRequester ,我再次不确定您是否必须创建一个FocusRequester list ,并且它们中的每一个都将提供给您的每个TextField

SideEffect {
        // I'm assuming some validation will run regarding the enabling of a textfield of yours
        if (someStateHandler[index].shouldBeEnabled) {
            focusRequester.requestFocus()
        }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM