当前位置:网站首页>Kotlin compose listens to the soft keyboard and clicks enter to submit the event

Kotlin compose listens to the soft keyboard and clicks enter to submit the event

2022-06-25 04:47:00 Ango cannot move

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun TodoInputText(
    text: String,
    onTextChanged: (String) -> Unit,
    onImeAction: () -> Unit,
    modifier: Modifier = Modifier,
) {

    val keyboardController = LocalSoftwareKeyboardController.current
    TextField(
        value = text,
        onValueChange = onTextChanged,
        modifier = modifier,
        colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.Transparent),
        maxLines = 1,
        // Configure soft keyboard 
        keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(onDone = {
            onImeAction()
            // Click finish   Hidden keyboard 
            keyboardController?.hide()
        })
    )
}

  Use this controller

LocalSoftwareKeyboardController

Just this and Flutter Different . No need to set it to the actual control

    onImeAction: () -> Unit, It is to input the things to be done by clicking "enter" .

Very human .

原网站

版权声明
本文为[Ango cannot move]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250335333623.html