以编程方式单击 Jetpack Compose 中的文本字段

Programmatically click textfield in jetpack compose

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

使用 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 { }
}