我想显示一个包含消息的对话框/弹出窗口,向用户展示“您确定要删除此条目吗?”并添加一个按钮,上面写着“删除”。当点击“删除”按钮时,它应该删除该条目,否则不会执行任何操作。
我已经为这些按钮编写了一个点击监听器,但是我该如何调用对话框或弹出窗口及其功能呢?
我想显示一个包含消息的对话框/弹出窗口,向用户展示“您确定要删除此条目吗?”并添加一个按钮,上面写着“删除”。当点击“删除”按钮时,它应该删除该条目,否则不会执行任何操作。
我已经为这些按钮编写了一个点击监听器,但是我该如何调用对话框或弹出窗口及其功能呢?
您可以使用 AlertDialog
来实现此功能,并使用其 Builder
类构建一个对话框。下面的示例使用只需要传入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();
尝试这段代码:
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
cancel()
等功能。 - Subbybuilder1.show()
直接显示结果没有问题,但是 builder1.create()
是否必要呢? - razzDavid 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();
}
}
});
create()
和show()
,因为show()
已经创建了描述内容的对话框。根据文档,create()
会使用提供给构建器的参数创建一个AlertDialog,但是它不会Dialog.show()该对话框。这允许用户在显示对话框之前执行任何额外的处理。如果您没有其他处理,并且想要创建并显示对话框,请使用show()。 因此,只有当您计划稍后显示对话框并预先加载其内容时,才有用使用create()
。 - Ruchir BaroniagetApplicationContext()
更改为 MyActivity.this
后,代码开始工作。 - O-9使用 AlertDialog.Builder :
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();
你将会得到以下输出。
一个简单的示例! 在Java类中创建对话框方法,类似于以下内容:
public void openDialog() {
final Dialog dialog = new Dialog(context); // Context, this, etc.
dialog.setContentView(R.layout.dialog_demo);
dialog.setTitle(R.string.dialog_title);
dialog.show();
}
dialog_demo.xml
并设计你的UI。这里是我为了演示目的而创建的一个样例:<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/dialog_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/dialog_text"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/dialog_info">
<Button
android:id="@+id/dialog_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="@color/dialog_cancel_bgcolor"
android:text="Cancel"/>
<Button
android:id="@+id/dialog_ok"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="@color/dialog_ok_bgcolor"
android:text="Agree"/>
</LinearLayout>
</RelativeLayout>
现在你可以随时调用openDialog()
了 :) 这是上述代码的截图。
请注意,文本和颜色都来自strings.xml
和colors.xml
。您可以定义自己的。
AlertDialog、DatePickerDialog或TimePickerDialog
(来自http://developer.android.com/guide/topics/ui/dialogs.html)。 - sweisgerber.dev现在最好使用DialogFragment而不是直接创建AlertDialog。
你可以使用这段代码:
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();
对我而言
new AlertDialog.Builder(this)
.setTitle("Closing application")
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("No", null).show();
new AlertDialog.Builder(context)
.setTitle("title")
.setMessage("message")
.setPositiveButton(android.R.string.ok, null)
.show();
// Dialog box
public void dialogBox() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Click on Image for tag");
alertDialogBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertDialogBuilder.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}