How do I display an alert dialog on Android?

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

    You can use this code:

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

提交回复
热议问题