简体   繁体   中英

Remove keypad along with ModalBottomSheetLayout in Jetpack compose

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)
    }
)

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() } } ) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM