Jetpack Compose ComponentDialog TextField cannot wake up the soft keyboard

80 Views Asked by At

I encountered an issue when mixing ComposeView, but I couldn't solve it very well.

When I was in ComposeView in ComponentDialogs, I found that TextField did not successfully wake up the soft keyboard after receiving focus.

ComponentDialog(this).apply {
    val contentView = ComposeView(context).apply {
        setContent {
            //Can pop up normally, but cannot wake up the soft keyboard when TextField gains focus
            var value by remember { mutableStateOf(TextFieldValue()) }
            TextField(
                value = value,
                onValueChange = {
                    value = it
                },
            )
        }
    }
    val layoutParams = ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT,
    )
    setContentView(contentView, layoutParams)
}.show()

But after wrapping a layer of Composite Dialogs inside, I was able to input content, but this would display two Dialogs, which made me very confused.

ComponentDialog(this).apply {
    val contentView = ComposeView(context).apply {
        setContent {
            //Can pop up normally, but cannot wake up the soft keyboard when TextField gains focus
            /*var value by remember { mutableStateOf(TextFieldValue()) }
            TextField(
                value = value,
                onValueChange = {
                    value = it
                },
            )*/

            //Can pop up normally and recall the soft keyboard normally
            Dialog(
                onDismissRequest = {
                    dismiss()
                },
            ) {
                var value by remember { mutableStateOf(TextFieldValue()) }
                TextField(
                    value = value,
                    onValueChange = {
                        value = it
                    },
                )
            }
        }
    }
    val layoutParams = ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT,
    )
    setContentView(contentView, layoutParams)
}.show()
0

There are 0 best solutions below