How do I display an alert dialog on Android?

后端 未结 30 2369
清歌不尽
清歌不尽 2020-11-22 03:02

I want to display a dialog/popup window with a message to the user that shows \"Are you sure you want to delete this entry?\" with one button that says \'Delete\'. When

相关标签:
30条回答
  • 2020-11-22 03:38

    Try this code:

    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();
    
    0 讨论(0)
  • 2020-11-22 03:38

    you can try this....

        AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
    dialog.setCancelable(false);
    dialog.setTitle("Dialog on Android");
    dialog.setMessage("Are you sure you want to delete this entry?" );
    dialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            //Action for "Delete".
        }
    })
            .setNegativeButton("Cancel ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                //Action for "Cancel".
                }
            });
    
    final AlertDialog alert = dialog.create();
    alert.show();
    

    For more info,check this link...

    0 讨论(0)
  • 2020-11-22 03:39
    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();
    }
    

    Also check out my blog on Dialogs in Android, you will find all the details here: http://www.fahmapps.com/2016/09/26/dialogs-in-android-part1/.

    0 讨论(0)
  • 2020-11-22 03:39

    Make this static method and use it where ever you want.

    public static void showAlertDialog(Context context, String title, String message, String posBtnMsg, String negBtnMsg) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle(title);
                builder.setMessage(message);
                builder.setPositiveButton(posBtnMsg, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.setNegativeButton(negBtnMsg, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
    
            }
    
    0 讨论(0)
  • 2020-11-22 03:40

    You can create the dialog box using AlertDialog.Builder

    Try this:

    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();
    

    To change the color of the positive & negative buttons of Alert dialog you can write the below two lines after alertDialog.show();

    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
    alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimaryDark));
    

    0 讨论(0)
  • 2020-11-22 03:43
    showDialog(MainActivity.this, "title", "message", "OK", "Cancel", {...}, {...});
    

    Kotlin

    fun showDialog(context: Context, title: String, msg: String,
                   positiveBtnText: String, negativeBtnText: String?,
                   positiveBtnClickListener: DialogInterface.OnClickListener,
                   negativeBtnClickListener: DialogInterface.OnClickListener?): AlertDialog {
        val builder = AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(msg)
                .setCancelable(true)
                .setPositiveButton(positiveBtnText, positiveBtnClickListener)
        if (negativeBtnText != null)
            builder.setNegativeButton(negativeBtnText, negativeBtnClickListener)
        val alert = builder.create()
        alert.show()
        return alert
    }
    

    Java

    public static AlertDialog showDialog(@NonNull Context context, @NonNull String title, @NonNull String msg,
                                         @NonNull String positiveBtnText, @Nullable String negativeBtnText,
                                         @NonNull DialogInterface.OnClickListener positiveBtnClickListener,
                                         @Nullable DialogInterface.OnClickListener negativeBtnClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(msg)
                .setCancelable(true)
                .setPositiveButton(positiveBtnText, positiveBtnClickListener);
        if (negativeBtnText != null)
            builder.setNegativeButton(negativeBtnText, negativeBtnClickListener);
        AlertDialog alert = builder.create();
        alert.show();
        return alert;
    }
    
    0 讨论(0)
提交回复
热议问题