2013-05-21 16 views
9

Jak wyświetlić okno komunikatu w Xamarin.Android? Jak uzyskać odpowiedź "tak" lub "nie" z okna komunikatu?Jak wyświetlić MessageBox w Monodroid

--- Aktualizacja:

myBtn.Click += (sender, e) => 
{ 
    new AlertDialog.Builder(this) 
    .SetMessage("hi") 
    .Show(); 
}; 

Odpowiedz

17

Można użyć klasy AlertDialog.Buider od wewnątrz Activity.

new AlertDialog.Builder(this) 
    .SetPositiveButton("Yes", (sender, args) => 
    { 
     // User pressed yes 
    }) 
    .SetNegativeButton("No", (sender, args) => 
    { 
     // User pressed no 
    }) 
    .SetMessage("An error happened!") 
    .SetTitle("Error") 
    .Show(); 
+0

Dzięki. To działa. Ale jak mogę sprawić, aby wyglądał lepiej jak MessageBox w telefonie z systemem Windows? MessageBox ma wysokość, szerokość i kolor tła. – MilkBottle

+1

Zobacz xml z [this] (http://stackoverflow.com/a/13763132/1411687) odpowiedzi i użyj konstruktora z [this] (http://stackoverflow.com/a/3119581/1411687) jeden. Zauważ, że przed poziomem API 11 musisz użyć 'ContextThemeWrapper', aby skomentować okno dialogowe, ponieważ nowszy konstruktor nie jest dostępny. –

2

Spróbuj tego:

public void ShowAlert (string str) 
     { 
      AlertDialog.Builder alert = new AlertDialog.Builder (this); 
      alert.SetTitle (str); 
      alert.SetPositiveButton ("OK", (senderAlert, args) => { 
       // write your own set of instructions 
       }); 

      //run the alert in UI thread to display in the screen 
      RunOnUiThread (() => { 
       alert.Show(); 
      }); 
     }