Buscar..


Parámetros

Método público de uso común Utilizar
SetTitle (String) Establece el título para el diálogo
SetIcon (Dibujable) Establecer icono para el diálogo de alerta
SetMessage (cadena) Establezca el mensaje para mostrar.
SetNegativeButton (String, EventHandler) Configure un oyente para que se invoque cuando se presiona el botón negativo del diálogo.
SetPositiveButton (String, EventHandler) Configure un oyente para que se invoque cuando se presiona el botón positivo del diálogo.
SetNeutralButton (String, EventHandler) Configure un oyente para que se invoque cuando se presiona el botón neutral del diálogo.
SetOnCancelListener (IDialogInterfaceOnCancelListener) Establece la devolución de llamada que se llamará si se cancela el diálogo.
SetOnDismissListener (IDialogInterfaceOnDismissListener) Establece la devolución de llamada que se llamará cuando el cuadro de diálogo se cierre por cualquier motivo.
Show() Crea un AlertDialog con los argumentos proporcionados a este constructor y Dialog.Show es el diálogo.

Observaciones


Requerimientos

Espacio de nombres: Android.App

Ensamblaje: Mono.Android (en Mono.Android.dll)

Versiones de montaje: 0.0.0.0


Constructores publicos

AlertDialog.Builder (Contexto): -

El constructor utiliza un contexto para este constructor y el AlertDialog que crea.

AlertDialog.Builder (Context, Int32): -

El constructor utiliza un contexto y un tema para este constructor y el AlertDialog que crea.


Usando Material Design AlertDialog

Para utilizar el moderno AlertDialog:

  1. Instale la biblioteca AppCompat de Support v7 desde los paquetes de NuGet
  2. Reemplace AlertDialog con Android.Support.V7.App.AlertDialog o agregue la siguiente declaración en la parte superior para hacer que su diálogo brille.
        using AlertDialog = Android.Support.V7.App.AlertDialog;

AlertDialog

// 1. Instantiate an AlertDialog.Builder with its constructor
// the parameter this is the context (usually your activity)
AlertDialog.Builder builder = new AlertDialog.Builder(this);

// 2. Chain together various setter methods to set the dialog characteristics 
builder.SetMessage(Resource.String.dialog_message)
       .SetTitle(Resource.String.dialog_title);

// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.Create();

dialog.Show();

Ejemplo de diálogo de alerta simple

Crearemos un simple diálogo de alerta en Xamarin.Android

Ahora teniendo en cuenta que ha seguido la guía de introducción de la documentación.

Debes tener la estructura del proyecto así:

Estructura del proyecto

Tu actividad principal debe verse así:

 public class MainActivity : Activity
 {
 int count = 1;
 
 protected override void OnCreate(Bundle bundle)
 {
 base.OnCreate(bundle);
 
 // Set our view from the "main" layout resource
 SetContentView(Resource.Layout.Main);
 
 // Get our button from the layout resource,
 // and attach an event to it
 Button button = FindViewById<Button>(Resource.Id.MyButton);
 
 button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
 }
 }

Ahora lo que haremos es, en lugar de agregar uno al contador al hacer clic en el botón, le preguntaremos al usuario si desea agregar o restar uno en un simple diálogo de alerta.

Y al hacer clic en el botón Positivo o negativo, tomaremos la acción.

 button.Click += delegate {
 AlertDialog.Builder alert = new AlertDialog.Builder(this);
 alert.SetTitle("Specify Action");
 alert.SetMessage("Do you want to add or substract?");

 alert.SetPositiveButton("Add", (senderAlert, args) =>
 {
 count++;
 button.Text = string.Format("{0} clicks!", count);
  });

  alert.SetNegativeButton("Substract", (senderAlert, args) =>
  {
  count--;
  button.Text = string.Format("{0} clicks!", count);
  });

  Dialog dialog = alert.Create();
      dialog.Show();
 };

captura de pantalla:

introduzca la descripción de la imagen aquí



Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow