How do I display an alert dialog on Android?

后端 未结 30 2520
清歌不尽
清歌不尽 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:28

    This is a basic sample of how to create an Alert Dialog :

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

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

    Try this code

    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();
    
    0 讨论(0)
  • Just be careful when you want to dismiss the dialog - use dialog.dismiss(). In my first attempt I used dismissDialog(0) (which I probably copied from some place) which sometimes works. Using the object the system supplies sounds like a safer choice.

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

    With Anko (official library from developers of Kotlin), you can simple use

    alert("Alert title").show()
    

    or more complex one:

    alert("Hi, I'm Roy", "Have you tried turning it off and on again?") {
        yesButton { toast("Oh…") }
        noButton {}
    }.show()
    

    To import Anko:

    implementation "org.jetbrains.anko:anko:0.10.8"
    
    0 讨论(0)
  • 2020-11-22 03:31

    Simplest Solution For Kotln Developers

    val alertDialogBuilder: AlertDialog.Builder = AlertDialog.Builder(requireContext())
        alertDialogBuilder.setMessage(msg)
        alertDialogBuilder.setCancelable(true)
    
        alertDialogBuilder.setPositiveButton(
            getString(android.R.string.ok)
        ) { dialog, _ ->
            dialog.cancel()
        }
    
        val alertDialog: AlertDialog = alertDialogBuilder.create()
        alertDialog.show()
    
    0 讨论(0)
  • 2020-11-22 03:33

    you may try this way also, it will provide you material style dialogs

    private void showDialog()
    {
        String text2 = "<font color=#212121>Medi Notification</font>";//for custom title color
    
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
        builder.setTitle(Html.fromHtml(text2));
    
        String text3 = "<font color=#A4A4A4>You can complete your profile now or start using the app and come back later</font>";//for custom message
        builder.setMessage(Html.fromHtml(text3));
    
        builder.setPositiveButton("DELETE", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                toast = Toast.makeText(getApplicationContext(), "DELETE", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();              
            }
        });
    
        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                toast = Toast.makeText(getApplicationContext(), "CANCEL", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });
        builder.show();
    }
    
    0 讨论(0)
提交回复
热议问题