How do I display an alert dialog on Android?

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

    This is definitely help for you. Try this code: On click of a button, you can put one, two or three buttons with an alert dialog...

    SingleButtton.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View arg0) {
            // Creating alert Dialog with one Button
    
            AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
    
            // Setting Dialog Title
            alertDialog.setTitle("Alert Dialog");
    
            // Setting Dialog Message
            alertDialog.setMessage("Welcome to Android Application");
    
            // Setting Icon to Dialog
            alertDialog.setIcon(R.drawable.tick);
    
            // Setting OK Button
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
    
                public void onClick(DialogInterface dialog,int which)
                {
                    // Write your code here to execute after dialog    closed
                    Toast.makeText(getApplicationContext(),"You clicked on OK", Toast.LENGTH_SHORT).show();
                }
            });
    
            // Showing Alert Message
            alertDialog.show();
        }
    });
    
    btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View arg0) {
            // Creating alert Dialog with two Buttons
    
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
    
            // Setting Dialog Title
            alertDialog.setTitle("Confirm Delete...");
    
            // Setting Dialog Message
            alertDialog.setMessage("Are you sure you want delete this?");
    
            // Setting Icon to Dialog
            alertDialog.setIcon(R.drawable.delete);
    
            // Setting Positive "Yes" Button
            alertDialog.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" Button
            alertDialog.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 Message
            alertDialog.show();
        }
    });
    
    btnAlertThreeBtns.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View arg0) {
            // Creating alert Dialog with three Buttons
    
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                    AlertDialogActivity.this);
    
            // Setting Dialog Title
            alertDialog.setTitle("Save File...");
    
            // Setting Dialog Message
            alertDialog.setMessage("Do you want to save this file?");
    
            // Setting Icon to Dialog
            alertDialog.setIcon(R.drawable.save);
    
            // Setting Positive Yes Button
            alertDialog.setPositiveButton("YES",
                new DialogInterface.OnClickListener() {
    
                    public void onClick(DialogInterface dialog,
                            int which) {
                        // User pressed Cancel button. Write Logic Here
                        Toast.makeText(getApplicationContext(),
                                "You clicked on YES",
                                Toast.LENGTH_SHORT).show();
                    }
                });
    
            // Setting Negative No Button... Neutral means in between yes and cancel button
            alertDialog.setNeutralButton("NO",
                new DialogInterface.OnClickListener() {
    
                    public void onClick(DialogInterface dialog,
                            int which) {
                        // User pressed No button. Write Logic Here
                        Toast.makeText(getApplicationContext(),
                                "You clicked on NO", Toast.LENGTH_SHORT)
                                .show();
                    }
                });
    
            // Setting Positive "Cancel" Button
            alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
    
                    public void onClick(DialogInterface dialog,
                            int which) {
                        // User pressed Cancel button. Write Logic Here
                        Toast.makeText(getApplicationContext(),
                                "You clicked on Cancel",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            // Showing Alert Message
            alertDialog.show();
        }
    });
    

提交回复
热议问题