如何在撰写中点击下面对话框之外的任意位置时关闭键盘

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

how to close the keybooard whenever tapped anywhere in dialog

val keyboardController = LocalSoftwareKeyboardController.current
    val activity = LocalContext.current as Activity

    Dialog(
        onDismissRequest = { onDismiss() },
        properties = DialogProperties(usePlatformDefaultWidth = false),
    ) {
        Surface(
            modifier = Modifier
                .pointerInput(Unit) {
                    detectTapGestures(onTap = {
                        keyboardController?.hide()
                    })
                }
                .padding(start = 20.dp, end = 20.dp),
            shadowElevation = 4.dp
        ) {
            Column(
                modifier = Modifier.padding(24.dp)
            ) {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
android android-jetpack-compose mobile-application
1个回答
0
投票

在 onDismiss 中你可以处理这个问题

val keyboardController = LocalSoftwareKeyboardController.current
val focusManager = LocalFocusManager.current


onDismissRequest = { onDismiss()
 keyboardController?.hide()
focusManager.clear() },

内柱 使用

Column(
modifier = Modifier
    .pointerInput(Unit) {
        detectTapGestures(onTap = {
keyboardController?.hide()
focusManager.clear()
        })
    }
)
© www.soinside.com 2019 - 2024. All rights reserved.