在 Jetpack compose 中删除键盘和 ModalBottomSheetLayout

发布于 2025-01-15 18:44:57 字数 1163 浏览 0 评论 0原文

在 BottomSheet 外部单击后,BottomSheet 将被隐藏,但键盘仍保持不变。 我想在 ModalBottomSheetLayout 中隐藏 BottomSheet 后立即删除键盘

单击撰写键盘中的文本弹出< /a> 点击底部工作表外部后,底部工作表消失,但键盘仍然存在

val state = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
val scope = rememberCoroutineScope()
ModalBottomSheetLayout(
    sheetContent = {
        Column {
            var text by remember { mutableStateOf("") }
            OutlinedTextField(
                value = text,
                label = { Text("Text") },
                onValueChange = { text = it }
            )
            var text1 by remember { mutableStateOf("") }
            OutlinedTextField(
                value = text1,
                label = { Text("Text1") },
                onValueChange = { text1 = it }
            )
            OutlinedButton(
                onClick = { click() },
            ) { Text(text = "SUBMIT") }
        }
    },
    sheetState = state,
    content = {
        Greeting(state = state, scope = scope)
    }
)

After clicking outside BottomSheet, BottomSheet is hidden but the keypad remains persistent.
I want to remove the keypad as soon as BottomSheet is hidden in ModalBottomSheetLayout

After clicking the text in compose keypad pops up
After clicking outside bottom sheet, bottom sheet disappears but keypad persists

val state = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
val scope = rememberCoroutineScope()
ModalBottomSheetLayout(
    sheetContent = {
        Column {
            var text by remember { mutableStateOf("") }
            OutlinedTextField(
                value = text,
                label = { Text("Text") },
                onValueChange = { text = it }
            )
            var text1 by remember { mutableStateOf("") }
            OutlinedTextField(
                value = text1,
                label = { Text("Text1") },
                onValueChange = { text1 = it }
            )
            OutlinedButton(
                onClick = { click() },
            ) { Text(text = "SUBMIT") }
        }
    },
    sheetState = state,
    content = {
        Greeting(state = state, scope = scope)
    }
)

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

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

发布评论

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

评论(2

倥絔 2025-01-22 18:44:57

您可以尝试在 ModalBottomSheet 隐藏时隐藏键盘:

val keyboard = LocalSoftwareKeyboardController.current
val sheetState = rememberModalBottomSheetState(
    initialValue = ModalBottomSheetValue.Hidden,
    confirmStateChange = {
    if (it == ModalBottomSheetValue.Hidden) {
       keyboard?.hide()
    }
    true
})
ModalBottomSheetLayout(
    sheetBackgroundColor = TinderTheme.colors.transparent,
    sheetState = sheetState,
...
)

You can try to hide the keyboard when ModalBottomSheet is hidden:

val keyboard = LocalSoftwareKeyboardController.current
val sheetState = rememberModalBottomSheetState(
    initialValue = ModalBottomSheetValue.Hidden,
    confirmStateChange = {
    if (it == ModalBottomSheetValue.Hidden) {
       keyboard?.hide()
    }
    true
})
ModalBottomSheetLayout(
    sheetBackgroundColor = TinderTheme.colors.transparent,
    sheetState = sheetState,
...
)
想你的星星会说话 2025-01-22 18:44:57

您需要为文本字段添加键盘选项和操作。键盘选项设置键盘类型和键盘上的操作。您传递 lambda 的 onAction 来隐藏底部工作表并清除焦点:

val sheetState = rememberModalBottomSheetState(
    skipHalfExpanded = true,
    initialValue = ModalBottomSheetValue.Hidden
)
val focusRequester = remember { FocusRequester() }

OutlinedTextField(
    value = text,
    label = { Text("Text") },
    onValueChange = { text = it },
    keyboardOptions = KeyboardOptions(
        keyboardType = KeyboardType.Number,
        imeAction = ImeAction.Done
    ),
    keyboardActions = KeyboardActions(
        onDone = {
            coroutineScope.launch {
                focusRequester.freeFocus()
                sheetState.hide()
            }
        }
    )
)

You need to add keyboard options and actions for the text fields. The keyboard options setup what type of keyboard and action you have on the keyboard. The onAction you pass the lambda to hide the bottom sheet and clear the focus:

val sheetState = rememberModalBottomSheetState(
    skipHalfExpanded = true,
    initialValue = ModalBottomSheetValue.Hidden
)
val focusRequester = remember { FocusRequester() }

OutlinedTextField(
    value = text,
    label = { Text("Text") },
    onValueChange = { text = it },
    keyboardOptions = KeyboardOptions(
        keyboardType = KeyboardType.Number,
        imeAction = ImeAction.Done
    ),
    keyboardActions = KeyboardActions(
        onDone = {
            coroutineScope.launch {
                focusRequester.freeFocus()
                sheetState.hide()
            }
        }
    )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文