personalizado - tipos de dialogos en android studio
¿Cómo puedo agregar un tercer botón a un Diálogo de alerta de Android? (4)
Agregue cualquier número de botones sin xml:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setItems(new CharSequence[]
{"button 1", "button 2", "button 3", "button 4"},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The ''which'' argument contains the index position
// of the selected item
switch (which) {
case 0:
Toast.makeText(context, "clicked 1", 0).show();
break;
case 1:
Toast.makeText(context, "clicked 2", 0).show();
break;
case 2:
Toast.makeText(context, "clicked 3", 0).show();
break;
case 3:
Toast.makeText(context, "clicked 4", 0).show();
break;
}
}
});
builder.create().show();
La API dice que el Diálogo de alerta puede tener uno, dos o tres botones, pero el SDK solo permite un botón positivo y negativo. ¿Cómo puedo agregar un tercer botón?
Cuando está creando el diálogo, agregue algo como esto al constructor:
builder = new AlertDialog.Builder(context);
builder.setTitle("Test");
builder.setIcon(R.drawable.icon);
builder.setMessage("test");
builder.setPositiveButton("Call Now",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
builder.setNeutralButton("Setup",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
context.startActivity(new Intent(context, Setup.class));
//dialog.cancel();
}
});
builder.setNegativeButton("Exit",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
builder.create().show();
Este fragmento de código debería ayudar a explicar los tres botones diferentes que puede usar:
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Dialog Button");
alertDialog.setMessage("This is a three-button dialog!");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Button 1 Text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//...
} });
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Button 2 Text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//...
}});
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Button 3 Text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//...
}});
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
// set title
alertDialogBuilder.setTitle("To Do List");
// set dialog message
alertDialogBuilder
.setMessage("What do you want?")
.setCancelable(false)
.setPositiveButton("Delete All", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
}).setNeutralButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();