How do I display an alert dialog on Android?

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

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

提交回复
热议问题