如何将请求焦点转移到 Jetpack compose 中的下一个文本字段

帕德尼亚·巴加特

我的要求是当我在输入后在文本字段中输入 1 位数字时,它应该转移到下一个文本字段。我到处得到这样的答案。

简而言之,我正在寻找类似的功能,如 android 中的 onTextChangeListener

在jetpack compose中请求关注TextField

阿比马纽

这段代码应该给出一个基本的想法。

代码

@Composable
fun OtpScreen() {
    val focusManager = LocalFocusManager.current
    val (digit1, setDigit1) = remember {
        mutableStateOf("")
    }
    val (digit2, setDigit2) = remember {
        mutableStateOf("")
    }
    val (digit3, setDigit3) = remember {
        mutableStateOf("")
    }
    val (digit4, setDigit4) = remember {
        mutableStateOf("")
    }
    LaunchedEffect(
        key1 = digit1,
    ) {
        if (digit1.isNotEmpty()) {
            focusManager.moveFocus(
                focusDirection = FocusDirection.Next,
            )
        }
    }
    LaunchedEffect(
        key1 = digit2,
    ) {
        if (digit2.isNotEmpty()) {
            focusManager.moveFocus(
                focusDirection = FocusDirection.Next,
            )
        }
    }
    LaunchedEffect(
        key1 = digit3,
    ) {
        if (digit3.isNotEmpty()) {
            focusManager.moveFocus(
                focusDirection = FocusDirection.Next,
            )
        }
    }

    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceAround,
    ) {
        OutlinedTextField(
            value = digit1,
            onValueChange = {
                setDigit1(it)
            },
            keyboardOptions = KeyboardOptions(
                keyboardType = KeyboardType.NumberPassword,
                imeAction = ImeAction.Next,
            ),
            modifier = Modifier.width(64.dp),
        )
        OutlinedTextField(
            value = digit2,
            onValueChange = {
                setDigit2(it)
            },
            keyboardOptions = KeyboardOptions(
                keyboardType = KeyboardType.NumberPassword,
                imeAction = ImeAction.Next,
            ),
            modifier = Modifier.width(64.dp),
        )
        OutlinedTextField(
            value = digit3,
            onValueChange = {
                setDigit3(it)
            },
            keyboardOptions = KeyboardOptions(
                keyboardType = KeyboardType.NumberPassword,
                imeAction = ImeAction.Next,
            ),
            modifier = Modifier.width(64.dp),
        )
        OutlinedTextField(
            value = digit4,
            onValueChange = {
                setDigit4(it)
            },
            keyboardOptions = KeyboardOptions(
                keyboardType = KeyboardType.NumberPassword,
                imeAction = ImeAction.Done,
            ),
            modifier = Modifier.width(64.dp),
        )
    }
}

在此处输入图像描述

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在flutter中将焦点转移到下一个文本字段?

在Flutter中将焦点从一个文本字段更改为下一个文本字段

如何在Jetpack Compose中获取onTouchEvent?

如果使用组件,如何将焦点转移到下一个输入?

Jetpack Compose中的AdjustViewBounds模拟

如何在Jetpack Compose中处理导航?

将焦点更改为颤动中的下一个文本字段

Flutter:如何将焦点转移到下一个 TextFormField?

Jetpack compose 中的 ConstraintLayout 问题

Jetpack Compose 中的共享表

导航到jetpack compose中的可组合时如何自动请求焦点到文本字段

如何构建 Jetpack Compose 项目?

如何清除 Jetpack Compose 中的文本字段值?

Android Jetpack Compose 中的滑块

Jetpack Compose 中的阴影

如何测量 Jetpack Compose 中的渲染时间?

如何在 Android jetpack Compose 中设置默认焦点项

在 Jetpack Compose 中单击按钮时如何更改文本值?

Jetpack Compose LazyRow 滚动,仅捕捉到下一个或上一个元素的开始

如何将焦点从文本的点击事件转移到 googleplaces 自动完成输入?

如何在jetpack compose中创建带有掩码的文本字段输入?

如何在运行时添加 texdfields 并在 android jetpack compose 中管理这些文本字段值?

Jetpack Compose 中的副作用

如何使用jetpack compose在行中等距文本字段?

如何在jetpack compose中将文本从排版设置为另一个

Jetpack Compose 如何删除 LazyColumn 中的焦点文本字段

Jetpack Compose 如何删除 LazyColumn 中的焦点文本字段

jetpack compose 中的内部填充

如何在 jetpack compose 中对多文本字段使用文本字段控件?