2

有没有办法以编程方式单击文本字段,所以当我的搜索屏幕弹出时,它会自动单击文本字段并弹出键盘。或者,有没有办法知道文本字段的触摸事件?

4

1 回答 1

1

有了1.0.x您可以将焦点放在组件上。
就像是:

var text by remember { mutableStateOf(TextFieldValue("text")) }
val focusRequester = FocusRequester()
val keyboardController = LocalSoftwareKeyboardController.current

val interactionSource = remember { MutableInteractionSource() }
val isFocused by interactionSource.collectIsFocusedAsState()

Column {
    TextField(
        value = text,
        onValueChange = {
            text = it
        },
        interactionSource = interactionSource,
        label = { Text("label") },
        modifier = Modifier
            // add focusRequester modifier
            .focusRequester(focusRequester)
            .onFocusChanged {
                if (isFocused) {
                    keyboardController?.show()
                }
            }
    )
}

接着:

DisposableEffect(Unit) {
    focusRequester.requestFocus()
    onDispose { }
}
于 2021-04-01T14:58:19.390 回答