【发布时间】:2021-10-19 09:54:03
【问题描述】:
我们可以在 Android 中为对话框设置一个监听器:
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode,KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
/* The user pressed back button - do whatever here.
Normally you dismiss the dialog like dialog.dismiss(); */
}
return true;
}
});
我们如何在 Jetpack Compose 中为 AlertDialog 做到这一点?
@Composable
private fun DisplayAlertDialog() {
val openDialog = remember { mutableStateOf(true) }
if (openDialog.value) {
AlertDialog(
onDismissRequest = { },
title = {
Text(
text = stringResource(id = R.string.settings),
fontSize = 18.sp
)
},
confirmButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text(stringResource(id = R.string.yes))
}
},
dismissButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text(stringResource(id = R.string.no))
}
},
backgroundColor = Color.White,
contentColor = Color.Black
)
}
}
【问题讨论】:
标签: android kotlin android-jetpack-compose