安卓。如何在 Jetpack compose 中将焦点设置在 TextField 上而不弹出键盘?

问题描述 投票:0回答:1

我需要将焦点设置在 OutlinedTextField 上,我已经使用 focusRequester 做到了这一点。但是默认情况下,我不需要弹出键盘,只有当用户按下 OutlinedTextField 本身时,键盘才会弹出。

所以基本上我想完全禁用键盘弹出窗口。只有在文本字段上手动按下才能弹出键盘,同时还能够设置焦点。

这是我的测试可组合项:

                    var fieldValue by remember { mutableStateOf("") }
                    val focusRequester = FocusRequester()
                    val softwareKeyboard = LocalSoftwareKeyboardController.current

                    var showOutlinedTextField1 by remember { mutableStateOf(true) }


                    Column {
                        if (showOutlinedTextField1) {
                            OutlinedTextField(
                                label = { Text("TEXT1") },
                                value = fieldValue,
                                singleLine = true,
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .focusRequester(focusRequester),
                                onValueChange = { enteredValue ->
                                    fieldValue = enteredValue
                                },
                                keyboardOptions = KeyboardOptions.Default.copy(
                                    imeAction = ImeAction.Done
                                ),
                                keyboardActions = KeyboardActions(
                                    onDone = {
                                        fieldValue = ""
                                        showOutlinedTextField1 = false
                                    }
                                )
                            )
                        } else {
                            OutlinedTextField(
                                label = { Text("TEXT2") },
                                value = fieldValue,
                                singleLine = true,
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .focusRequester(focusRequester),
                                onValueChange = { enteredValue ->
                                    fieldValue = enteredValue
                                },
                                keyboardOptions = KeyboardOptions.Default.copy(
                                    imeAction = ImeAction.Done
                                ),
                                keyboardActions = KeyboardActions(
                                    onDone = {
                                        fieldValue = ""
                                        showOutlinedTextField1 = true
                                    }
                                )
                            )
                        }
                    }


                    DisposableEffect(showOutlinedTextField1) {
                        focusRequester.requestFocus()
                        softwareKeyboard?.hide()
                        onDispose { }
                    }

我尝试了 LocalSoftwareKeyboardController.current?.hide() 但它并不总是有效,当它起作用时,键盘首先弹出,然后才隐藏。

android kotlin android-jetpack-compose
1个回答
0
投票

同样的问题已回答这里, 只需使用 CompositionLocalProvider 包裹您的文本字段,如下所示。

CompositionLocalProvider(
        LocalTextInputService provides null
    ) {
          OutlinedTextField(
                 label = { Text("TEXT1") },
                 value = fieldValue,
                 singleLine = true,
                 modifier = Modifier
                             .fillMaxWidth()
                             .focusRequester(focusRequester),
                 onValueChange = { enteredValue ->
                                fieldValue = enteredValue
                            },
                 keyboardOptions = KeyboardOptions.Default.copy(
                                imeAction = ImeAction.Done
                            ),
                  keyboardActions = KeyboardActions(
                                onDone = {
                                    fieldValue = ""
                                    showOutlinedTextField1 = false
                                }
                            )
            )
     }
© www.soinside.com 2019 - 2024. All rights reserved.