Presentation is loading. Please wait.

Presentation is loading. Please wait.

GUI og XML programmering Hvor meget kode og hvor meget XML?

Similar presentations


Presentation on theme: "GUI og XML programmering Hvor meget kode og hvor meget XML?"— Presentation transcript:

1 GUI og XML programmering Hvor meget kode og hvor meget XML?

2 Agenda •Clicker applikation med knapper i stede for ListActivity! •Applikation med flere Activities og Views –Data fra en Activity til en anden •Forberedelse til RESTFul service opslag –Brug af Android Service ->Tilføjelse af Android Preferences (Host adresse)

3 Motivation Sådan en apps skal vi også have

4 Android Application Class  Svarer ”nogenlunde” til J2ME Midlet  “Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created.”  Kan håndterer onCreate, onTerminate, onLowMemory og onConfigurationChanged events

5 Eksempel Application class // ** The Application Class *************************************** // // Appliction Class Extenstion import android.app.Application; import android.content.res.Configuration; public class MyApplication extends Application { private static MyApplication singleton; // Returns the application instance public static MyApplication getInstance() { return singleton; } @Override public final void onCreate() { super.onCreate(); singleton = this; } // Manifest entry <application android:icon="@drawable/icon" android:name="MyApplication"> [... Manifest nodes...]

6 Android Activity Class (Android Activities)  Brugergrænseflade og brugeraktioner er en aktivitet // ** The Activity Class ****************************************** // package com.paad.myapplication; import android.app.Activity; import android.os.Bundle; public class MyActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } // ** Activity layout in XML <activity android:label="@string/app_name" android:name=".MyActivity">

7 7 | 2010 Activity bruger View og Layout til UI og interaktion med brugeren •Activity og Form svarer til hinanden: Præsentere et skærmbillede 1:1 •Activity bruger Views, Layout og Widgets/Controls (Standard eller egne) •Der findes en sæt af specielt designede Activities i forhold til standard widgets •MapActivty, List Activty, ExpandableListActivty og TabActivity •Tilstand styret af Android Framework. •Mange Activities i en applikation kan give behov for eget Application objekt. “This hierarchy tree can be as simple or complex as you need it to be, and you can build it up using Android's set of predefined widgets and layouts, or with custom Views that you create yourself”. Screen Layout

8 8 | 2010 Klassehieraki

9 Activity ->Layout-> View -> Widget &| ->UI Control  View er adgangen til skærmressourcen på enheden  Layout er manageren, der kontrollere View opsætningen  Widget er en kontrol i View, og som ligner den rigtige verdens ting. Et ur eller et kompas. Kan også være et View  UI control er grafiske enheder som knapper eller ”gestures” @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); TextView myTextView = (TextView)findViewById(R.id.myText View); } @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); TextView myTextView = new TextView(this); setContentView(myTextView); myTextView.setText("Hello, Android"); }

10 Klassediagram Et eksempel Fra http://www.droidnova.com/playing-with-graphics-in-android-part-i,147.htmlhttp://www.droidnova.com/playing-with-graphics-in-android-part-i,147.html

11 Layout hvordan XML og kode <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Enter Text Below" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Text Goes Here!" /> LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); TextView myTextView = new TextView(this); EditText myEditText = new EditText(this); myTextView.setText("Enter Text Below"); myEditText.setText("Text Goes Here!"); int lHeight = LinearLayout.LayoutParams.FILL_PARENT; int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT; ll.addView(myTextView, new LinearLayout.LayoutParams(lHeight, lWidth)); ll.addView(myEditText, new LinearLayout.LayoutParams(lHeight, lWidth)); setContentView(ll);

12 Hvad er der så at holde styr på i GUI’en? http://developer.android.com/guide/topics/ui/index.html http://developer.android.com/guide/topics/ui/index.html  View http://developer.android.com/reference/android/view/View.html http://developer.android.com/reference/android/view/View.html  ViewGroup http://developer.android.com/reference/android/view/ViewGroup.html http://developer.android.com/reference/android/view/ViewGroup.html  Layout http://developer.android.com/reference/android/widget/LinearLayout.html http://developer.android.com/reference/android/widget/LinearLayout.html  Widget Package http://developer.android.com/reference/android/widget/package- summary.html http://developer.android.com/reference/android/widget/package- summary.html  Menu http://developer.android.com/guide/topics/ui/menus.html (Menu knappen) http://developer.android.com/guide/topics/ui/menus.html  View properties: Statisk og/eller dynamisk.  UI Events  Define an event listener and register it with the View  Override an existing callback method for the View (Custom Views)  Menu Events

13 Event Listners http://tseng-blog.nge-web.net/blog/2009/02/14/implementing-listeners-in-your-android- java-application/ http://tseng-blog.nge-web.net/blog/2009/02/14/implementing-listeners-in-your-android- java-application/ 1.Inline Clas Implementation 2.Bruge “Implements” metoden 3.Bruge en variabel til en listner metode 4.XML attribute android:onClick="click1” •Sørg for at have en void click1(View v){ …} i Activity

14 Event Listners http://tseng-blog.nge-web.net/blog/2009/02/14/implementing-listeners-in-your-android- java-application/ http://tseng-blog.nge-web.net/blog/2009/02/14/implementing-listeners-in-your-android- java-application/ 1.Inline Clas Implementation 2.Bruge “Implements” metoden 3.Bruge en variabel til en listner metode 4.XML attribute android:onClick="click1” •Sørg for at have en void click1(View v){ …} metode i Activity

15 Data fra en Activity til en anden 1.Intent og Events 2.Content Provider 3.Shared Preferences 4.SQLLite 5.Filsystemet 6.En extern server 7.?? 1.I samme Apps ? 2.I forskellige Apps? 3.På forskellige Smartphones

16 Samme App Intent intent = new Intent(); intent.putExtra(BackDoorMan.REQ_Q_A, mes); intent.setClassName("dk.euc.clicker", "dk.euc.clicker.SecActivtity"); try { startActivity(intent); } catch (Exception e) { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.clicker); question = getIntent().getExtras().getString(BackDoorMan.REQ_Q_A);

17 Preferences og opsætning 1.Er tilknyttet egen Acitivity en xml fil og “hooks” i Preferences public class Preferences extends PreferenceActivity { public static final String PREF_CLICKER_HOST = "PREF_CLICKER_HOST"; SharedPreferences prefs; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.samplepreferences); } <EditTextPreference android:key="PREF_CLICKER_HOST" android:title="@string/sample_pref_txt_dialog" android:dialogTitle="@string/sample_pref_txt_dialog" android:summary="Preference Summary" />

18 Preferences Motodev Wizard

19 Preferences internt opret // Retrieve an editor to modify the shared preferences. SharedPreferences.Editor editor = mySharedPreferences.edit(); // Store new primitive types in the shared preferences object. editor.putBoolean("isTrue", true); editor.putFloat("lastFloat", 1f); editor.putInt("wholeNumber", 2); editor.putLong("aNumber", 3l); editor.putString("textEntryValue", "Not Empty"); // Commit the changes. editor.commit();

20 Preferences internt læs public void loadPreferences() { // Get the stored preferences int mode = Activity.MODE_PRIVATE; SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, mode); // Retrieve the saved values. boolean isTrue = mySharedPreferences.getBoolean("isTrue", false); float lastFloat = mySharedPreferences.getFloat("lastFloat", 0f); int wholeNumber = mySharedPreferences.getInt("wholeNumber", 1); long aNumber = mySharedPreferences.getLong("aNumber", 0); String stringPreference = mySharedPreferences.getString("textEntryValue", ""); }

21


Download ppt "GUI og XML programmering Hvor meget kode og hvor meget XML?"

Similar presentations


Ads by Google