我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
当前回答
new AlertDialog.Builder(v.getContext()).setMessage("msg to display!").show();
其他回答
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/。
您可以使用AlertDialog创建对话框。构建器
试试这个:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to delete this entry?");
builder.setPositiveButton("Yes, please", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//perform any action
Toast.makeText(getApplicationContext(), "Yes clicked", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//perform any action
Toast.makeText(getApplicationContext(), "No clicked", Toast.LENGTH_SHORT).show();
}
});
//creating alert dialog
AlertDialog alertDialog = builder.create();
alertDialog.show();
要改变Alert对话框的正反按钮的颜色,你可以在alertDialog.show()后面写下面两行;
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimaryDark));
我想补充David Hedlund的回答,分享一个比他发布的更有活力的方法,这样当你有消极行为要做的时候,你可以使用它,当你没有的时候,我希望它能有所帮助。
private void showAlertDialog(@NonNull Context context, @NonNull String alertDialogTitle, @NonNull String alertDialogMessage, @NonNull String positiveButtonText, @Nullable String negativeButtonText, @NonNull final int positiveAction, @Nullable final Integer negativeAction, @NonNull boolean hasNegativeAction)
{
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
builder.setTitle(alertDialogTitle)
.setMessage(alertDialogMessage)
.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (positiveAction)
{
case 1:
//TODO:Do your positive action here
break;
}
}
});
if(hasNegativeAction || negativeAction!=null || negativeButtonText!=null)
{
builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (negativeAction)
{
case 1:
//TODO:Do your negative action here
break;
//TODO: add cases when needed
}
}
});
}
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.show();
}
试试Kotlin
AlertDialog.Builder(this)
.setTitle("Title")
.setPositiveButton("Yes"){ dialog, which ->
}
.setNegativeButton("No", null)
.setMessage("Your given alert message...")
.show()
试试这段代码
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
// set title
alertDialogBuilder.setTitle("AlertDialog Title");
// set dialog message
alertDialogBuilder
.setMessage("Some Alert Dialog message.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(this, "OK button click ", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("CANCEL",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(this, "CANCEL button click ", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();