[Xamarin – Android] How To Use Dialog (2)
April 2, 2014 Leave a comment
From post : https://sabitlabscode.wordpress.com/2014/03/30/xamarin-android-how-to-use-dialog-1/ , we will learn how to make a dialog with multi choice item. Multi choice means we can choose more than one item from the list. It will show as checkbox, not radio button. From previous project, call dialog with this code :
MultiChoiceItem Dialog
protected override Dialog OnCreateDialog (int id) { switch (id) { case yourId: { string[] color_options = Resources.GetStringArray (Resource.Array.nameArray); bool[] check_list; // this use for make default check in list check_list = new bool[color_options.Length]; // it set the length of the list same with length options check_list [2] = true; // this is how we set the list, // you can set where list that you want to set with // set the index builder = new AlertDialog.Builder (this); builder.SetTitle ("Pick a color"); builder.SetNegativeButton ("Cancel", CancelClicked); builder.SetPositiveButton ("OK", OkClicked);// make event ok separate in another event // it set the dialog with multi choice item, and if one of the item click/select // app will set check_list value builder.SetMultiChoiceItems (color_options, check_list, (o, e) => { check_list [e.Which] = e.IsChecked; }); return builder.Create (); } } return null; }
After that, we need to make “OkClicked” to run the event when user click “OK” button.
private void OkClicked (object sender, DialogClickEventArgs e) { // insert your code here // you can get the value from check_list like "check_list[0]" ((Dialog)sender).Dismiss (); }
Input Dialog
Input dialog is a dialog that you can input with a text not select an item. With input dilaog, get spesific value to process because it not only some options. To implement it, you just need to insert your code like this :
protected override Dialog OnCreateDialog (int id) { switch (id) { case yourId: { EditText input = new EditText (this); builder = new AlertDialog.Builder (this); builder.SetTitle ("Insert Value: "); builder.SetMessage ("Enter your text here"); builder.SetView (input); builder.SetPositiveButton ("Ok", (o, e) => { // insert your code here ((Dialog)o).Dismiss (); }); builder.SetNegativeButton ("Cancel", Cancelcancel); return builder.Create (); } } return null; }
After insert that code, run your application and you will see input text on your dialog..
Finish… Hope it helps..
Happy coding…
Komentar