How do I display an alert dialog on Android?

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

    Use AlertDialog.Builder :

    AlertDialog alertDialog = new AlertDialog.Builder(this)
    //set icon 
     .setIcon(android.R.drawable.ic_dialog_alert)
    //set title
    .setTitle("Are you sure to Exit")
    //set message
    .setMessage("Exiting will call finish() method")
    //set positive button
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
       //set what would happen when positive button is clicked    
            finish();
        }
    })
    //set negative button
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
       //set what should happen when negative button is clicked
            Toast.makeText(getApplicationContext(),"Nothing Happened",Toast.LENGTH_LONG).show();
        }
    })
    .show();
    

    You will get the following output.

    To view alert dialog tutorial use the link below.

    Android Alert Dialog Tutorial

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

    Alert dialog with edit text

    AlertDialog.Builder builder = new AlertDialog.Builder(context);//Context is activity context
    final EditText input = new EditText(context);
    builder.setTitle(getString(R.string.remove_item_dialog_title));
            builder.setMessage(getString(R.string.dialog_message_remove_item));
     builder.setTitle(getString(R.string.update_qty));
                builder.setMessage("");
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
                input.setLayoutParams(lp);
                input.setHint(getString(R.string.enter_qty));
                input.setTextColor(ContextCompat.getColor(context, R.color.textColor));
                input.setInputType(InputType.TYPE_CLASS_NUMBER);
                input.setText("String in edit text you want");
                builder.setView(input);
       builder.setPositiveButton(getString(android.R.string.ok),
                    (dialog, which) -> {
    
    //Positive button click event
      });
    
     builder.setNegativeButton(getString(android.R.string.cancel),
                    (dialog, which) -> {
    //Negative button click event
                    });
            AlertDialog dialog = builder.create();
            dialog.show();
    
    0 讨论(0)
  • 2020-11-22 03:35

    for me

    new AlertDialog.Builder(this)
        .setTitle("Closing application")
        .setMessage("Are you sure you want to exit?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
    
              }
         }).setNegativeButton("No", null).show();
    
    0 讨论(0)
  • 2020-11-22 03:36

    Nowadays it's better to use DialogFragment instead of direct AlertDialog creation.

    • How? See: https://stackoverflow.com/a/21032871/1390874
    • Why? See: https://stackoverflow.com/a/13765411/1390874
    0 讨论(0)
  • 2020-11-22 03:36

    Code to delete an entry from the list

     /*--dialog for delete entry--*/
    private void cancelBookingAlert() {
        AlertDialog dialog;
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogCustom);
        alertDialog.setTitle("Delete Entry");
        alertDialog.setMessage("Are you sure you want to delete this entry?");
        alertDialog.setCancelable(false);
    
        alertDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
               //code to delete entry
            }
        });
    
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
    
        dialog = alertDialog.create();
        dialog.show();
    }
    

    Call above method on delete button click

    0 讨论(0)
  • 2020-11-22 03:37
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("This is Title");
        builder.setMessage("This is message for Alert Dialog");
        builder.setPositiveButton("Positive Button", (dialog, which) -> onBackPressed());
        builder.setNegativeButton("Negative Button", (dialog, which) -> dialog.cancel());
        builder.show();
    

    This is a way which alike to create the Alert dialog with some line of code.

    0 讨论(0)
提交回复
热议问题