在启用/禁用JetPack组成的TextField之后,如何管理TextField Focus?

发布于 2025-02-04 06:41:33 字数 1029 浏览 2 评论 0 原文

我正在与Textfield合作,并且对重点遇到了一些问题。

禁用所有TextField,仅启用了第一个。当试图启用其他Textfield时,它失去了焦点,我们如何获得重点? ScreenShot_20220602_121247 这是我的布局: -

在启用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 am working with TextField, and I have faced some issues regarding focus.

Disabled all TextField, only the first one is enabled. When trying to enable other TextField, it lost focus, how we can get back focus?
Screenshot_20220602_121247
Here is my layout:-
enter image description here

How to shift focus programmatically when enabling TextField?

Here is my code snippet which I have used to enable and disable the 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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈年往事 2025-02-11 06:41:33

在处理焦点管理时,我还没有尝试过此代码,但是有一个由修改器传播的功能,您可以在其中管理一些焦点属性,尽管我不确定这是否有效,并且如果您需要明确提供 focusrequster box 的儿童

 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 上也提供了一些便利,并为其 movefocus < /code>功能

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

猜测您的代码/逻辑,调用 focusrequester 在Textfield的修改器上提供的,我不确定您是否必须创建 list < /代码> focusrequester ,每个代码将提供给您的每个 textfield

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

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

 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

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

SideEffect {
        // I'm assuming some validation will run regarding the enabling of a textfield of yours
        if (someStateHandler[index].shouldBeEnabled) {
            focusRequester.requestFocus()
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文