android 在Jetpack合成中移除小键盘沿着ModalBottomSheetLayout

x7rlezfr  于 2023-03-16  发布在  Android
关注(0)|答案(2)|浏览(143)

在BottomSheet外部单击后,BottomSheet将隐藏,但小键盘仍保留。我希望在ModalBottomSheetLayout中隐藏BottomSheet后立即删除小键盘
After clicking the text in compose keypad pops upAfter 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)
    }
)
4c8rllxm

4c8rllxm1#

需要为文本字段添加键盘选项和操作,键盘选项设置键盘类型和键盘上的操作,传递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()
            }
        }
    )
)
8tntrjer

8tntrjer2#

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,
...
)

相关问题