Created
September 7, 2021 16:22
-
-
Save ruyut/8e8edd6756e1c73a7dd2a20c13572058 to your computer and use it in GitHub Desktop.
Jetpack Compose AlertDialog彈出式視窗
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.ruyut.jetpackcompose | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.material.AlertDialog | |
import androidx.compose.material.Button | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.* | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
ClickableText() | |
} | |
} | |
} | |
@Composable | |
fun ClickableText() { | |
var showAlertDialog by remember { mutableStateOf(false) } | |
Button(onClick = { showAlertDialog = true }) { | |
Text("Show AlertDialog") | |
} | |
if (showAlertDialog) { | |
AlertDialog( | |
onDismissRequest = { | |
// 點擊 彈出視窗 外的區域觸發 | |
showAlertDialog = false; | |
}, | |
title = { | |
Text("RuyutAlertDialog") | |
}, | |
text = { | |
Text("This is a dialog.") | |
}, | |
confirmButton = { | |
Button( | |
onClick = { | |
showAlertDialog = false | |
} | |
) | |
{ | |
Text(text = "確認按鈕") | |
} | |
}, | |
dismissButton = { | |
Button(onClick = { showAlertDialog = false }) | |
{ | |
Text(text = "取消按鈕") | |
} | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment