120 likes | 234 Views
Activity og GUI Android Brugergrænseflade. Android Application Class. Svarer ”nogenlunde” til J2ME Midlet
E N D
Activity og GUI Android Brugergrænseflade
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 <application> tag, which will cause that class to be instantiated for you when the process for your application/package is created.” • Kan håndtereronCreate, onTerminate, onLowMemoryogonConfigurationChanged events
Eksempel Applicationclass // ** The ApplicationClass *************************************** // // ******************************************************************* // ** Listing 3-7: Skeletonapplicationclass // ApplictionClassExtenstion import android.app.Application; import android.content.res.Configuration; public classMyApplicationextendsApplication { private staticMyApplication singleton; // Returns the applicationinstance public staticMyApplicationgetInstance() { return singleton; } @Override public final voidonCreate() { super.onCreate(); singleton = this; } } // Manifest entry <application android:icon="@drawable/icon" android:name="MyApplication"> [... Manifestnodes ...] </application>
Android Activity Class (Android Activities) • Brugergrænseflade og brugeraktioner er en aktivitet // ** The ActivityClass ****************************************** // // ** Listing 3-9: Activityskeletoncode packagecom.paad.myapplication; import android.app.Activity; import android.os.Bundle; public classMyActivityextendsActivity { /** Calledwhen the activity is firstcreated. */ @Override public voidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); } } // ** Listing 3-10: Activity layout in XML <activityandroid:label="@string/app_name" android:name=".MyActivity"> </activity>
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. Screen Layout “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”.
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” // ** Listing 4-2: Creating a UI layout in code @Override public voidonCreate(Bundleicicle) { super.onCreate(icicle); TextViewmyTextView = new TextView(this); setContentView(myTextView); myTextView.setText("Hello, Android"); } // ** Listing 4-1: Inflating an Activity layout @Override public voidonCreate(Bundleicicle) { super.onCreate(icicle); setContentView(R.layout.main); TextViewmyTextView = (TextView)findViewById(R.id.myTextView); }
Klassediagram Et eksempel Fra http://www.droidnova.com/playing-with-graphics-in-android-part-i,147.html
Layout hvordan XML ogkode // ** Listing 4-3: Simple Linear Layout in XML <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns: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="EnterTextBelow" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextGoesHere!" /> </LinearLayout> // ** Listing 4-4: Simple LinearLayout in code LinearLayoutll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); TextViewmyTextView = new TextView(this); EditTextmyEditText = new EditText(this); myTextView.setText("EnterTextBelow"); myEditText.setText("TextGoesHere!"); intlHeight = LinearLayout.LayoutParams.FILL_PARENT; intlWidth = LinearLayout.LayoutParams.WRAP_CONTENT; ll.addView(myTextView, new LinearLayout.LayoutParams(lHeight, lWidth)); ll.addView(myEditText, new LinearLayout.LayoutParams(lHeight, lWidth)); setContentView(ll);
Hvad er der så at holde styr på i GUI’en?http://developer.android.com/guide/topics/ui/index.html • View http://developer.android.com/reference/android/view/View.html • ViewGrouphttp://developer.android.com/reference/android/view/ViewGroup.html • Layout http://developer.android.com/reference/android/widget/LinearLayout.html • Widget Package http://developer.android.com/reference/android/widget/package-summary.html • Menu http://developer.android.com/guide/topics/ui/menus.html(Menu knappen) • UI Events • Define an event listener and register it with the View • Override an existing callback method for the View (Custom Views) • Menu Events
Event Listnershttp://tseng-blog.nge-web.net/blog/2009/02/14/implementing-listeners-in-your-android-java-application/ Inline Classe Implementation Bruge “Implements” metoden Bruge en variabeltil en listnermetode XML attribute android:onClick="click1"