Skip to content

Instantly share code, notes, and snippets.

@hoshi-takanori
Created March 7, 2021 22:00
Show Gist options
  • Save hoshi-takanori/c230b6b89810fce76596dffdda89aea8 to your computer and use it in GitHub Desktop.
Save hoshi-takanori/c230b6b89810fce76596dffdda89aea8 to your computer and use it in GitHub Desktop.
AlertDialog with TextField doesn't work in Jetpack Compose 1.0.0-beta01
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
@Composable
fun ComposeEditDialog() {
val message = remember { mutableStateOf("Edit Me") }
val openDialog = remember { mutableStateOf(false) }
val editMessage = remember { mutableStateOf("") }
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = message.value,
fontSize = 24.sp
)
Spacer(modifier = Modifier.height(32.dp))
Button(
onClick = {
editMessage.value = message.value
openDialog.value = true
}
) {
Text("Open Dialog")
}
}
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
openDialog.value = false
},
title = {
Text(text = "Input Message")
},
text = {
TextField(
value = editMessage.value,
onValueChange = { editMessage.value = it },
singleLine = true
)
},
confirmButton = {
Button(
onClick = {
message.value = editMessage.value
openDialog.value = false
}
) {
Text("OK")
}
},
dismissButton = {
Button(
onClick = {
openDialog.value = false
}
) {
Text("Cancel")
}
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment