【发布时间】:2022-07-16 23:55:23
【问题描述】:
我尝试在其他应用程序上显示警报对话框。已授予权限。
<uses-permission android:name="ACTION_MANAGE_OVERLAY_PERMISSION"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Composable 方式根本行不通,代码如下:
AlertDialog(onDismissRequest = { dialogEnabled.value = false },
title = { Text(text = "Test")},
text = {
Text(text = message)
},
confirmButton = {
TextButton(onClick = { dialogEnabled.value = false }) {
Text(text = "Confirm")
}
},
dismissButton = {
TextButton(onClick = { dialogEnabled.value = false }) {
Text(text = "Cancel")
}
}
)
我必须将 AlertDialog 的窗口类型设置为 WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY。但是,我查看AndroidDialog.android.kt下的AlertDialog的代码,没有办法得到对话框实例的引用,甚至没有DialogProperty。
最终,我用传统的方式来实现它
private fun showDialog(message: String){
val builder: AlertDialog.Builder = AlertDialog.Builder(this) //set icon
.setIcon(android.R.drawable.ic_dialog_alert) //set title
.setTitle("Game Analysis") //set message
.setMessage(message) //set positive button
.setPositiveButton(
"Confrim"
) { dialogInterface, i -> //set what would happen when positive button is clicked
dialogInterface.dismiss()
} //set negative button
.setNegativeButton(
"Cancel"
) { dialogInterface, i -> //set what should happen when negative button is clicked
dialogInterface.dismiss()
}
val alertDialog: AlertDialog = builder.create()
alertDialog.window!!.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
alertDialog.show()
}
【问题讨论】:
标签: android android-alertdialog android-jetpack-compose