Android

Transkrypt

Android
Programowanie
urządzeń mobilnych
dr inż. Juliusz Mikoda
poniedziałek, 8 listopada 2010
Instalacja środowiska 1
• Wymagane
• Eclipse
•
składniki środowiska
– wersja 3.4 (Ganymede) lub 3.5 (Galileo classic)
http://www.eclipse.org/downloads/packages/release/galileo/r
• Zainstalowany
• Android
•
wtyczka ADT (Android Development Tools)
SDK (Software Developer Kit)
http://developer.android.com/sdk/index.html
poniedziałek, 8 listopada 2010
Instalacja środowiska 2
• Instalacja
•
•
•
•
•
ADT
Po uruchomieniu eclipsa
należy wybrać z menu:
Help ⇒ Install New
Software
Następnie Add...
Nazwa: dowolna
(np.:
Android)
Location: https://dlssl.google.com/android/
eclipse/
Zatwierdzenie: OK
poniedziałek, 8 listopada 2010
Instalacja środowiska 3
• Instalacja
•
ADT
Po załadowaniu dostępnych
pakietów należy wybrać:
Android DDMS
• Android Development Tools
•
Następnie Next
• Po zaakceptowaniu
licencji Finish
•
poniedziałek, 8 listopada 2010
Instalacja środowiska 4
• Instalacja
• Wskazanie
SDK
ścieżki roboczej SDK:
• Windows: W
ustawieniach systemowych (prawy przycisk na:
Mój komputer i Właściwości) w zakładce Zaawansowane
pod przyciskiem zmienne środowiskowe należy dopisać do
zmiennej PATH: ;<katalog_SDK>/tools
• Linux: export
PATH=${PATH}:<katalog_SDK>/
tools Można wprowadzić w pliku .bash_profile lub .bashrc
poniedziałek, 8 listopada 2010
Instalacja środowiska 5
• Konfiguracja
ADT i SDK
• Menu: Window
⇒ Preferences opcja Android należy
ustawić ścieżkę SDK Location według miejsca instalacji
SDK (bez /tools)
• Menu: Window
Manager:
• Instalacja
bibliotek androida Available Packages
• Tworzenie
poniedziałek, 8 listopada 2010
⇒ Android SDK and AVD
wirtualnych urządzeń Virtual Devices
Instalacja środowiska 6
poniedziałek, 8 listopada 2010
Instalacja środowiska 7
poniedziałek, 8 listopada 2010
Pierwszy program
File ⇒ New ⇒ Projekt…
poniedziałek, 8 listopada 2010
Android ⇒ Android Project
Next
Pierwszy program
poniedziałek, 8 listopada 2010
Pierwszy program – konstrukcja
Zawartość utworzonego
projektu:
• src – katalog pakietów – kodu
programu
• gen – dane generowane przez
kompilator na podstawie
zasobów
• assest – dodatki – katalog
plików niekompilowalnych
• res – zasoby
• AndroidManifest.xml – plik
konfiguracyjny Aplikacji android
•
poniedziałek, 8 listopada 2010
Pierwszy program – zasoby
•
•
•
•
•
•
•
res ⇒value
łańcuchy tekstowe
definicje kolorów, elementów rysowanych
definicja rozmiarów
definicja styli
definicja tablic tekstowych / liczbowych
…
poniedziałek, 8 listopada 2010
Pierwszy program – układ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android
="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent„
android:layout_height="fill_parent„
android:orientation="vertical" >
<TextView android:layout_height="wrap_content"
android:text="@string/hello„
android:layout_width="wrap_content„ />
</LinearLayout>
•
Elementy pozycjonujące
•
TableLayout
•
LinearLayout
•
RelativeLayout
•
AbsoluteLayout
•
FrameLayout
poniedziałek, 8 listopada 2010
•
Kontrolki
• TextView
• EditText
• Button
• AnalogClock
• DigitalClock
•…
Pierwszy program – R.java
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
poniedziałek, 8 listopada 2010
Pierwszy program – Start
package pl.example.android.HelloAndroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
poniedziałek, 8 listopada 2010
Tworzenie wyglądu okna
poniedziałek, 8 listopada 2010
Układ okna – LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent„
android:layout_height="fill_parent„
android:orientation="vertical" >
…….
</LinearLayout>
• Rozmieszczenie
•
•
•
•
•
poniedziałek, 8 listopada 2010
elementów podrzędnych w
poziomie i w pionie
android:layout_width="fill_parent"
• pełne wypełnienie elementu nadrzędnego
android:layout_height="wrap_conten"
• dopasowane wypełnienie elementu nadrzędnego
• 10px
android:orientation="vertical"
• kierunek układania elementów
android:layout_gravity="center_horizontal"
• położenie komponentu na oknie
android:gravity="center_horizontal"
• położenie tekstu w komponencie (EditText)
Układ okna – TableLayout
Rozmieszczenie elementów
podrzędnych w postaci tabeli
• android:collapseColumns
• liczba elementów w kolejnych
wierszach
• android:stretchColumns="0,1,2"
• równomierne rozciągnięcie kolumn
• Element zml’a <TableRow> ... </
TableRow> określa jeden wiersz
rozmieszczenia na ekranie
•
poniedziałek, 8 listopada 2010
Układ okna – TableLayout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<EditText android:text="Bez Row" />
<TableRow>
<TextView android:text="Nazwisko" />
<EditText android:text="N1" />
<EditText android:text="N2" />
</TableRow>
<TableRow>
<TextView android:text="Pesel" />
<EditText android:text="Text" />
</TableRow>
<TableRow>
<EditText android:text="N1" android:layout_height="150px"/>
<EditText android:text="N2" />
<EditText android:text="N3" android:layout_gravity="bottom"/>
<EditText android:text="N4" />
<EditText android:text="N5" />
</TableRow>
</TableLayout>
poniedziałek, 8 listopada 2010
Układ okna – RelativeLayout
Rozmieszczenie elementów
podrzędnych względem
innych elementów lub układu
nadrzędnego
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/
res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:layout_marginLeft="10px"
android:text="Wprowadź tekst:" />
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:layout_below="@id/label" />
poniedziałek, 8 listopada 2010
Układ okna – RelativeLayout
<Button android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:text="Wycofaj" />
<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/cancel"
android:layout_alignTop="@+id/cancel"
android:text="Zapisz" />
</RelativeLayout>
poniedziałek, 8 listopada 2010
Id elementu:
android:layout_above
android:layout_alignBaseline
android:layout_alignBottom
android:layout_alignLeft
android:layout_alignRight
android:layout_alignTop
android:layout_below
android:layout_toLeftOf
android:layout_toRightOf
Wartość boolean:
android:layout_alignParentBottom
android:layout_alignParentLeft
android:layout_alignParentRight
android:layout_alignParentTop
android:layout_alignWithParentIfMissing
android:layout_centerHorizontal
android:layout_centerInParent
android:layout_centerVertical
Układ okna – FrameLayout
Pozwala wyświetlić elementy, gdzie <FrameLayout
dla każdego elementu bazą będzie android:layout_width="fill_parent„
android:layout_height="fill_parent">
lewy, górny róg ekranu.
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src
="@drawable/golden_gate" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity
="center_horizontal|bottom"
android:layout_marginBottom="20dip"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Golden Gate" />
</FrameLayout>
poniedziałek, 8 listopada 2010
Układ okna – AbsoluteLayout
Pozwala wyświetlić elementy w
określonych współrzędnych
ekranu. Elementy mogą
zachodzić na siebie.
poniedziałek, 8 listopada 2010
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android ="http://
schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_x="20px"
android:layout_y="20px"
android:text="20x20" />
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_y="80px"
android:layout_x="40px"
android:text="40x80" />
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_x="60px"
android:layout_y="50px"
android:text="60x50" />
</AbsoluteLayout>
Kontrolki TextView
Kontrolka TextView pozwala na
wyświetlanie tekstu lecz nie
zezwala na jego edycję.
<TextView android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Wprowadzony tekst" />
poniedziałek, 8 listopada 2010
Przydatne atrybuty:
android:text
android:textColor
android:textSize
android:textStyle
android:typeface
android:gravity
android:background
android:layout_height
android:layout_width
android:layout_margin
android:layout_marginBottom
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
Kontrolki EditText
Kontrolka EditText pozwala na
pobranie i edycję tekstu przez
użytkownika.
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
poniedziałek, 8 listopada 2010
Przydatne atrybuty:
android:password
android:phoneNumber
android:numeric
android:capitalize
android:inputType="textPassword"
Metody:
Editable getText()
setText(CharSequence text,
TextView.BufferType type)
Kontrolki Button
Kontrolka Button pozwala na
umieszczenie przycisku na
ekranie urządzenia.
<Button android:text="Przycisk 1"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px" />
Obsługa naciśnięcia przycisku:
public class MainActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final Button button = (Button)
findViewById(R.id.Button01);
button.setOnClickListener(new
View.OnClickListener() {
public void onClick(View v) {
// action on click
}
});
}
}
Lub
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/self_destruct"
android:onClick="selfDestruct" />
public void selfDestruct(View view) {
// action on click
}
poniedziałek, 8 listopada 2010

Podobne dokumenty