Lab thuc hanh Android

  • View
    794

  • Download
    0

Embed Size (px)

Transcript

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Lab 1: ng dng Android u tin

Mc tiuLm quen vi cch thc to ng dng Android c bn dng IDE Eclipse. Hiu cu trc c bn ca Android project. Dng XML to layout ca Activity. Quen vi vic s dng cc resource trong ng dng Android.

Yu cu ci t mi trng y xy dng ng dng Android trn Eclipse. C mt s kin thc c bn v lp trnh Android.

Hng dn1. Bc 1: To ng dng Android t Eclipse Trong Eclipse chn Alt +Shift + N ( New project), chn tip Android Project

Nguyn H Giang (nguyenha.giang@yahoo.com)

1

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Hnh 1.1: Minh ho cch to Android Project Sau khi khai bo cc thng tin to mi Android project th chn Finish hon tt. Eclipse s to mt project Android c cu trc nh sau:

Hnh 1.2: Ton b Android project ban u c Eclipse pht sinh ng dng ny ch c duy nht mt thnh phn gi l Activity c tn l FirstAppAndroidActivity, trong ng dng Android, activity l thnh phn GUI cha cc widget (tng t nh control trong windows form). Ni mt cch tng qut ng dng nu c tng tc vi ngi dng thng qua UI th phi c activity, trong ng dng Android c th to ra nhiu Activity (ging nh to nhiu form trong lp trnh desktop). Trong Activity FirstAppAndroidActivity trn th c phng thc override onCreate phng thc ny dng khi to thnh phn UI v cc x l ca activity. Trong phng thc ny c gi hm setContentView v truyn vo l id ca layout c khai bo trong th mc res/layout

Nguyn H Giang (nguyenha.giang@yahoo.com)

2

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Hnh 1.3: File XML Layout cha m t giao din ca activity Gii thch file m t layout main.xml ca activity: Bao gm mt LinearLayout, y l dng ViewGroup cho php cha cc View bn trong v c sp xp theo hai dng: vertical hay horizontal. Trong layout ny LinearLayout c thit lp theo phng dc, gi tr fill_parent cho bit layout s chim ht kch thc ca thnh phn bao cha n (full kch thc). Mt TextView l mt dng tng t nh Label trong Windows Form, cho php hin th ni dung thng tin no , TextView ny c thit lp c kch thc ngang l kch thc ca thnh phn bao cha, v kch thc di l wrap, va hin th ni dung. Thuc tnh android:text thit lp chui cn hin th trn TextView, trong phn ny khai bo chui l @string/hello c ngha l ly chui tn hello c khai bo trong phn resource l file strings.xml, khi ni dung (gi tr) ca chui hello s hin th ln trn TextView.

Nguyn H Giang (nguyenha.giang@yahoo.com)

3

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Hnh 1.4: File strings.xml cha nh ngha cc chui File strings.xml cha cc nh ngha lin quan n chui, khi lp trnh trn Android nn s dng file ny nh ngha cc chui v trong chng trnh Java hay phn layout s tham chiu n cc chui ny. Cch truy xut chui khai bo trong strings.xml c m t nh hnh di

Hnh 1.5: M t cch thc tham chiu n chui trong java code v XML layout.

Nguyn H Giang (nguyenha.giang@yahoo.com)

4

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

2. Bc 2: Bin dch v chy ng dng u tin ta c kt qu trn emulator nh sau:

Hnh 1.6: ng dng khi chy trn emulator 3. Bc 3: Modify li chng trnh hin th thng bo sau: y l chng trnh Android u tin ca ti.

Nguyn H Giang (nguyenha.giang@yahoo.com)

5

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Hnh 1.7: ng dng sau khi modify li chui 4. Bc 4: lm quen vi cc thuc tnh ca TextView, thit lp cc thuc tnh cho TextView theo bng sau (thit lp trong file layout xml). Thit lp thuc tnh cho TextView trong file layout XML textSize textColor textStyle gravity shadowColor shadowRadius shadowDx shadowDy 30dp #ff5500 bold center #e6b121 1.5 1 1

Nguyn H Giang (nguyenha.giang@yahoo.com)

6

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Kt qu c activity nh sau (trong demo ny thay i text ca TextView l Hello Android:

Hnh 1.8: Kt qu sau khi thit lp cc thuc tnh ca TextView Trong phn khai bo mu ca textColor v shadowColor ta dng hng s mu, vic dng trc tip nh vy i khi kh hiu (khi nhn vo m hexa khng bit mu g), ta c th lm cch khc d hiu hn bng cch to file resource nh ngha bng mu. Trong Android cho php lm iu ny bng cch khai bo file colors.xml nh hnh minh ho sau: Trong file ny ta nh ngha hai mu nh sau: #ff5500 #e6b121 Khi tham chiu trong layout th dng c php sau

Nguyn H Giang (nguyenha.giang@yahoo.com)

7

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Thit lp thuc tnh cho TextView trong file layout XML textColor shadowColor @color/orange @color/gold

Hnh 1.9: Mn hnh b sung file nh ngha hng s mu trong resource 5. Bc 5: thm hnh nn vo trong linearlayout Import mt hnh nn no vo project, (cch thc import hng dn trong phn lab v J2ME)

Nguyn H Giang (nguyenha.giang@yahoo.com)

8

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Hnh 1.10: Import hnh lm nh nn vo project Khai bo hnh nn cho LinearLayout nh sau Kt qu c ng dng nh sau: ( i ni dung ca TextView l Welcome to Lng C Beach

Nguyn H Giang (nguyenha.giang@yahoo.com)

9

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Hnh 1.11: Giao din ca ng dng sau khi b sung hnh nn 6. B sung TextView hin th ni dung bn phi, di ca layout, nh hnh minh ho sau hin th c nh vy th y ta dng dng layout l RelativeLayout, vi kiu layout ny th cc thnh phn bn trong s c t v tr tng i so vi cha v cc phn view bn trong. Code bn di l phn m t layout trong main.xml

Nguyn H Giang (nguyenha.giang@yahoo.com)

10

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Hnh 1.12: Phn layout s dng RelativeLayout.

Nguyn H Giang (nguyenha.giang@yahoo.com)

11

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Hnh 1.13: Kt qu khi dng RelativeLayout. 7. Bc 7: minh ho to activity th 2 trong ng dng ny, activity th 2 ny c giao din cho php user nhp vo tn trong mt EditText v sau kch vo button, ng dng s xut ra mt ca s nh pop-up hin cu cho. Bc 7.1: To mt mt activity mi c tn SecondActivity: kch chut phi vo th mc src ca project chn New ->Class, trong ca s New Java Class khai bo tn ca lp activity v khai bo lp c s l Activity

Nguyn H Giang (nguyenha.giang@yahoo.com)

12

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Hnh 1.14: To mi lp activity th hai trong ng dng Lp SecondActivity c pht sinh vi source code nh sau:

Hnh 1.15: Source code ca SecondActivity

Nguyn H Giang (nguyenha.giang@yahoo.com)

13

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Bc 7.2: To file layout cha phn m t giao din ca SecondActivity: layout ny l dng Relative gm c mt EditText v mt Button cha bn trong. File layout ny c tn l second.xml. Cch to file second.xml nh sau: kch chut phi vo th mc layout, chn New -> Android XML File

Hnh 1.16: To file XML layout cho SecondActivity File second.xml ban u c m t nh sau

Nguyn H Giang (nguyenha.giang@yahoo.com)

14

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Bc 7.3: b sung EditText v Button vo second layout nh m t sau

Gii thch: EditText android:id="@+id/EditText01" android:hint="Nhp h tn..." android:layout_alignParentLeft="true" android:layout_width="fill_parent" android:layout_height="wrap_content" Khai bo id ca EditText Xut hin khi ni dung empty Canh l tri vi parent Fill kch thc ngang Wrap dc

android:layout_toLeftOf="@+id/Button01" Canh bn tri view c id l Button01 Button android:id="@+id/Button01" android:text="Xin cho!" android:layout_width="wrap_content" Khai bo id ca Button Caption ca Button Wrap ni dung

Nguyn H Giang (nguyenha.giang@yahoo.com)

15

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

android:layout_height="wrap_content" android:layout_alignParentRight="true" android:onClick="showMe"

Wrap ni dung Canh l bn phi parent. Khai bo hm x l s kin khi click

Chuyn qua Graphical layout xem layout.

Hnh 1.17: Graphical layout ca activity SecondActivity Bc 7.4: nh ngha hm x l s kin click ca button trong lp activity (SecondActivity).

Bc 7.5: override phng thc onCreate ca SecondActivity, trong phng thc onCreate load layout ln giao din ca activity.

Nguyn H Giang (nguyenha.giang@yahoo.com)

16

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Bc 7.6: Cu hnh trong AndroidManifest.xml, khai bo activity mi v thit lp ng dng hin th activity th 2.

Hnh 1.18: B sung m t SecondActivity vo androidmanifest.xml Chuyn th intent-filter t activity 1 xung phn khai bo ca activity th 2.

Hnh 1.19: Khai bo SecondActivity c hin th khi ng dng chy Bc 7.7: bin dch v chy ng dng

Nguyn H Giang (nguyenha.giang@yahoo.com)

17

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Hnh 1.20: Giao din tng tc ca ng dng vi SecondActivity.

M rng1. Vit li ng dng trn khng dng XML m t giao din ca cc activity m dng code Java thc hin. 2. To mt activity c giao din nh sau:

Hnh 1.21: Giao din activityNguyn H Giang (nguyenha.giang@yahoo.com) 18

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Trong cc mu c nh ngha nh sau: #f00 #ffa500 #ffff00 #0f0 #00f #4b0082 #ee82ee #000 #fff 3. Vit ng dng n gin cho php user nhp vo hai s v chn mt trong cc php ton {+,*,/} thc hin, chng trnh tnh kt qu v hin th ln mn hnh.

Hnh 1.22: Giao din ng dng basic calc

Nguyn H Giang (nguyenha.giang@yahoo.com)

19

Lp trnh trn thit b di ng: Android

Khoa CNTT - Hutech

Hng dn: s dng widget Spinner (tng t nh thnh phn combobox quen thuc), Spinner ny c thuc tnh entries ly danh sch chui lm mc chn, danh sch chui ny c nh ngha l mng chui: trong strings.xml.

Hnh 1.23: M t cch s dng Spinner