Kotlin Compose 监听软键盘 点击enter提交事件

@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,
        //配置软键盘
        keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(onDone = {
            onImeAction()
            //点击完成之后 隐藏键盘
            keyboardController?.hide()
        })
    )
}

 使用这个controller

LocalSoftwareKeyboardController

单是这个和Flutter不同。不用设置到实际的控件当中

    onImeAction: () -> Unit, 就是输入完成点击回车要做的事情。

非常的人性化。

猜你喜欢

转载自blog.csdn.net/mp624183768/article/details/125454429