0

聚焦时会打印键,TextFieldBox如果聚焦则不会打印键本身。

Box(
    modifier = Modifier.onKeyEvent {
        println(it.key)
        false
    }.fillMaxSize().focusable()
) {
    val fieldValue = remember { mutableStateOf(TextFieldValue("")) }
    TextField(
        value = fieldValue.value,
        onValueChange = { fieldValue.value = it }
    )
}
4

1 回答 1

0

受此答案的启发,我更改了代码。

当您单击 时Box,您会从 中移除焦点,TextField但不会将其交给Box。这必须手动完成。

val focusRequester = FocusRequester()

Box(
    modifier = Modifier.onKeyEvent {
        println(it.key)
        false
    }.fillMaxSize()
        .focusRequester(focusRequester)
        .focusable()
        .clickable (
            interactionSource = remember { MutableInteractionSource() },
            indication = null // To disable the ripple effect
        ) {
            focusRequester.requestFocus()
        }
) {
    val fieldValue = remember { mutableStateOf(TextFieldValue("")) }
    TextField(
        value = fieldValue.value,
        onValueChange = { fieldValue.value = it }
    )
}
于 2021-08-22T18:43:27.147 回答