wtorek, 3 lutego 2015

Kulisy Xamarina - odc. 3: jeszcze trochę o Androidzie

Trochę informacji uzupełniających odnośnie Androida i Xamarina. W ogólnym ujęciu należy podkreślić, że obok wynikowej dll-ki generowane są wrappery w postaci klas Java i w konsekwencji plik APK. W szczegółach nic nadzwyczajnego, kolejne przykłady na używanie w C# funkcjonalności znanych z Android SDK (Java).

 

Ogólne uzupełnienia

Aktywności Java i C# mogą się wzajemnie wywoływać, po obu stronach mamy odpowiedniki.

Z assembly .NET generowane są wrappery w Javie (JNI) i budowany jest plik APK.

.NET BCL + Android SDK + Xamarin.Mobile (np. geolokalizacja)

Możliwość używania bibliotek  Javy i .NET

http://developer.xamarin.com/releases/android/

 

Drobne uzupełnienia

Odczytanie napisu z zasobów:

GetString(Resource.String.Xyz)

Wypisanie na konsolę (Output w VS):

Console.WriteLine(“xxx”);

Tworzenie adaptera z tablicy stringów w zasobach (dla Spinner):

var adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.categories, Android.Resource.Layout.SimpleSpinnerItem);

adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);

Pobranie elementu Spinnera o danym indeksie:

spinner.GetItemAtPosition(position);

 

Ustawienia

Plik xml w Resources/Xml

[Activity(Label = “Preferences”)]

public class PreferencesActivity: PreferenceActivity

{

         protected override void OnCreate(Bundle bundle)

         {

                 base.OnCreate(bundle);

                 AddPreferencesFromResource(Resource.Xml.preferences);

         }

}

 

[Activity(Label = “XXX”, MainLauncher = true, Icon = “@drawable/icon”)]

public class XActivity: Activity

{

         …

         protected override void OnResume()

         {

                  base.OnResume();

                  var preferences = PreferenceManager.GetDefaultPreferences(ApplicationContext);

                  var x = preferences.GetString(“x”, “0”);

                  …

         }

         //zapis ustawień

         …

         var preferences = PreferenceManager.GetDefaultPreferences(ApplicationContext);

         var editor = preferences.Edit();

          editor.PutString(“x”, “0”);

          editor.Commit();

}

 

Menu

option menu

plik XML w Resources/Menu

 

[Activity(Label = “XXX”, MainLauncher = true, Icon = “@drawable/icon”)]

public class XActivity: Activity

{

         …

         public override bool OnCreateOptionsMenu(IMenu menu)

         {

                   MenuInflater.Inflate(Resource.Menu.optionsmenu, menu);

                   menu.Add(“YYY”);

                   return true;

         }

         public override bool OnOptionsItemSelected(IMenuItem item)

         {

                  //item.TitleFormatted

                  switch(item.ItemId)

                  {

                           case Resource.Id.xxx:

                                      …

                                      return true;

                            default:

                                       return base.OnOptionsItemSelected(item);

                  }               

         }

}

menu kontekstowe

plik XML w Resources/Menu

 

[Activity(Label = “XXX”, MainLauncher = true, Icon = “@drawable/icon”)]

public class XActivity: Activity

{

        Button button;

         …

         protected override void OnCreate(Bundle bundle)

         {

                  …

                  RegisterForContextMenu(button);

          }

          public override void OnCreateContextMenu(IContextMenu menu, View v,

          IContextMenuContextMenuInfo menuInfo)

          {

                   base.OnCreateContextMenu(menu, v, menuInfo);

                   if (v == button)

                   {

                           MenuInflater.Inflate(Resource.Menu.contextmenu, menu);

                   }

          }

          public override bool OnContextItemSelected(IMenuItem item)

          {

                    switch(item.ItemId)

                    {

                           case Resource.Id.Aaa:

                                  …

                                  //przy wybieraniu:  item.SetChecked(true);

                                  return true;

                            default:

                                  return base.OnContextItemSelected(item);

                    }

          }

   }

 

Dialogi

[Activity(Label = “XXX”, MainLauncher = true, Icon = “@drawable/icon”)]

public class XActivity: Activity

{

        private int const ALERT_DIALOG = 0;

        …

        protected override Dialog OnCreateDialog(int id)

        {

                 switch(id)

                 {

                         case ALERT_DIALOG:

                                  var builder = new AlertDialog.Builder(this);                                 

                                  /* return builder.SetTitle(“Xxxx ?”)

                                          .SetMessage(“Yyyy zzzz aaa bbb ?”)

                                          .SetCancelable(false)

                                          .SetPositiveButton(“OK”, (o, args) => { … })

                                          .SetNegativeButton(“No”, (o, args) => { … })

                                          .Create(); */                                  

                                   

                                   //.SetItems(), .SetMultiChoiceItems()

                                   return builder.SetTitle(“Xxxx ?”)                                        

                                          .SetCancelable(false)

                                          .SetPositiveButton(“OK”, (o, args) => { … })

                                          .SetNegativeButton(“No”, (o, args) => { … })

                                          .SetItems(new [] { “XXX”, “YYY”, “ZZZ”},  (o, args) => {  //args.Which   })

                                          .Create();

                 }

                 return null;

        }

        …

        ShowDialog(ALERT_DIALOG);   

        …

        var progressDialog = ProgressDialog.Show(this, “Tytuł”, “Wiadomość”);

        ThreadPool.QueueUserWorkItem(s =>

        {

                 Thread.Sleep(5000);

                 RunOnUiThread(() => progressDialog.Dismiss());

        });

}

niestandardowy dialog

plik XML w Resources/Layout

w OnCreateDialog:

var dialog = new Dialog(this);

dialog.SetContentView(Resource.Layout.customdialog);

var button = (Button) dialog.FindViewById(Resource.Id.customButton);

button.Text = “XXX”;

return dialog;

DatePicker / TimePicker dialog

w OnCreateDialog:

var dateDialog = new DatePickerDialog(this, (o, args) => { /*args.Date*/ }, 2015, 2, 3);

return dateDialog;

 

Publikacja

http://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/publishing_an_application/part_1_-_preparing_an_application_for_release/

Manifest Android w katalogu Properties projektu

Brak komentarzy: