为什么 focusManager 在 Jetpack Compose 中不起作用?

发布于 2025-01-09 15:20:49 字数 929 浏览 0 评论 0原文

我正在尝试使用 imeAction onNext 和 focusDirection.down 将焦点从用户文本字段移动到密码文本字段,但是当我按下键盘的下一个按钮时,焦点被清除。 UI 图像

我在此处显示代码:

val focusManager = LocalFocusManager.current
//we start to implement login screen UI-->
    LazyColumn(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        item {
            
        item {
            OutlinedTextField(

                value = emailValue.value,
                keyboardOptions = KeyboardOptions(
                    imeAction = ImeAction.Next,
                    keyboardType = KeyboardType.Password
                ),
                keyboardActions = KeyboardActions(
                    onNext ={
                        focusManager.moveFocus(FocusDirection.Down)
                    }

                ),
    

I am trying move focus from user textfield to password textfield with imeAction onNext and focusDirection.down, but the focus is cleared when I press the next button of the keyboard.
UI image

I show the code here:

val focusManager = LocalFocusManager.current
//we start to implement login screen UI-->
    LazyColumn(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        item {
            
        item {
            OutlinedTextField(

                value = emailValue.value,
                keyboardOptions = KeyboardOptions(
                    imeAction = ImeAction.Next,
                    keyboardType = KeyboardType.Password
                ),
                keyboardActions = KeyboardActions(
                    onNext ={
                        focusManager.moveFocus(FocusDirection.Down)
                    }

                ),
    

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

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

发布评论

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

评论(1

网名女生简单气质 2025-01-16 15:20:49

设置 singleLine ,请尝试

OutlinedTextField(
...
singleLine = true,
)

简单的示例

@Composable
fun Test() {
    val focusManager = LocalFocusManager.current
    var text1 by remember {
        mutableStateOf("")
    }
    LazyColumn() {
        items(2){
            OutlinedTextField(value = text1, onValueChange = {
                text1 = it
            },
                keyboardOptions = KeyboardOptions(
                    imeAction = ImeAction.Next,
                    keyboardType = KeyboardType.Text
                ),
                keyboardActions = KeyboardActions(
                    onNext ={
                        focusManager.moveFocus(FocusDirection.Down)
                    }
                ),
                singleLine = true
            )
        }
    }
}

Set singleLine , please try

OutlinedTextField(
...
singleLine = true,
)

simple example

@Composable
fun Test() {
    val focusManager = LocalFocusManager.current
    var text1 by remember {
        mutableStateOf("")
    }
    LazyColumn() {
        items(2){
            OutlinedTextField(value = text1, onValueChange = {
                text1 = it
            },
                keyboardOptions = KeyboardOptions(
                    imeAction = ImeAction.Next,
                    keyboardType = KeyboardType.Text
                ),
                keyboardActions = KeyboardActions(
                    onNext ={
                        focusManager.moveFocus(FocusDirection.Down)
                    }
                ),
                singleLine = true
            )
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文