我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
当前回答
这是在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()
其他回答
你可以使用下面的代码:
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(
AlertDialogActivity.this);
// Setting Dialog Title
alertDialog2.setTitle("Confirm Delete...");
// Setting Dialog Message
alertDialog2.setMessage("Are you sure you want delete this file?");
// Setting Icon to Dialog
alertDialog2.setIcon(R.drawable.delete);
// Setting Positive "Yes" Btn
alertDialog2.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(),
"You clicked on YES", Toast.LENGTH_SHORT)
.show();
}
});
// Setting Negative "NO" Btn
alertDialog2.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(),
"You clicked on NO", Toast.LENGTH_SHORT)
.show();
dialog.cancel();
}
});
// Showing Alert Dialog
alertDialog2.show();
David Hedlund发布的代码给了我错误:
无法添加窗口-空令牌无效
如果你得到同样的错误,请使用下面的代码。它的工作原理!
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!isFinishing()){
new AlertDialog.Builder(YourActivity.this)
.setTitle("Your Alert")
.setMessage("Your Message")
.setCancelable(false)
.setPositiveButton("ok", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Whatever...
}
}).show();
}
}
});
使用AlertDialog。建造者:
AlertDialog alertDialog = new AlertDialog.Builder(this)
//set icon
.setIcon(android.R.drawable.ic_dialog_alert)
//set title
.setTitle("Are you sure to Exit")
//set message
.setMessage("Exiting will call finish() method")
//set positive button
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what would happen when positive button is clicked
finish();
}
})
//set negative button
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what should happen when negative button is clicked
Toast.makeText(getApplicationContext(),"Nothing Happened",Toast.LENGTH_LONG).show();
}
})
.show();
您将得到以下输出。
要查看警报对话框教程使用下面的链接。
Android警报对话框教程
在材质组件库中,你可以使用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")
}
}
)
}
public void showSimpleDialog(View view) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(false);
builder.setTitle("AlertDialog Title");
builder.setMessage("Simple Dialog Message");
builder.setPositiveButton("OK!!!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//
}
})
.setNegativeButton("Cancel ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// Create the AlertDialog object and return it
builder.create().show();
}
也可以看看我的关于Android对话的博客,你会在这里找到所有的细节:http://www.fahmapps.com/2016/09/26/dialogs-in-android-part1/。