我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
当前回答
在材质组件库中,你可以使用MaterialAlertDialogBuilder
MaterialAlertDialogBuilder(context)
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton("Delete") { dialog, which ->
// Respond to positive button press
}
.setNegativeButton("Cancel") { dialog, which ->
// Respond to positive button press
}
.show()
使用Compose 1.0。X你可以使用:
val openDialog = remember { mutableStateOf(true) }
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
// Dismiss the dialog when the user clicks outside the dialog or on the back
// button. If you want to disable that functionality, simply use an empty
// onCloseRequest.
openDialog.value = false
},
title = null,
text = {
Text(
"Are you sure you want to delete this entry?"
)
},
confirmButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text("Delete")
}
},
dismissButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text("Cancel")
}
}
)
}
其他回答
您可以为此使用AlertDialog,并使用它的Builder类构造一个AlertDialog。下面的例子使用默认的构造函数,它只接受Context,因为对话框将从传入的Context继承适当的主题,但是如果你想这样做,还有一个构造函数允许你指定一个特定的主题资源作为第二个参数。
new AlertDialog.Builder(context)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Continue with delete operation
}
})
// A null listener allows the button to dismiss the dialog and take no further action.
.setNegativeButton(android.R.string.no, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
现在最好使用DialogFragment而不是直接创建AlertDialog。
怎么做?参见:https://stackoverflow.com/a/21032871/1390874 为什么?参见:https://stackoverflow.com/a/13765411/1390874
new AlertDialog.Builder(context)
.setTitle("title")
.setMessage("message")
.setPositiveButton(android.R.string.ok, null)
.show();
这是在kotlin中完成的
val builder: AlertDialog.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert)
} else {
AlertDialog.Builder(this)
}
builder.setTitle("Delete Alert!")
.setMessage("Are you want to delete this entry?")
.setPositiveButton("YES") { dialog, which ->
}
.setNegativeButton("NO") { dialog, which ->
}
.setIcon(R.drawable.ic_launcher_foreground)
.show()
从列表中删除条目的代码
/*--dialog for delete entry--*/
private void cancelBookingAlert() {
AlertDialog dialog;
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogCustom);
alertDialog.setTitle("Delete Entry");
alertDialog.setMessage("Are you sure you want to delete this entry?");
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//code to delete entry
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = alertDialog.create();
dialog.show();
}
在删除按钮单击时调用上述方法