You are on page 1of 6

Bi 2- Chng trnh u tin (Tng / Gim mt con s)

Yu cu: phi thc hin bi 1 c c project t c: chng trnh c 1 thng bo xut kt qu ban u l 0. 1 Nt tng nhn vo s tng kt qu ln 1. 1 nt gim nhn vo s gim kt qu xung. 1. M file main.xml trong ng dn myproject1/res/layout

Ta thy bc bn ngoi l mt LinearLayout (layout theo kiu tng dng) . C thuc tnh: android:layout_width v android:layout_height : chiu rng v cao ca i tng. N c 3 gi tr l: o fill_parent : to bng cha ca n. (c i tn thnh match_parent t API level 8) o match_parent: ging fill_parent o wrap_content: to va ni dung m n cha. android:orientation: nh hng (vertical: thng ng, horizontal: nm ngang) Bn trong l mt TextView c thuc tnh l: Android:text: l ni dung ca TextView, y c th nh 1 chui trc tip hoc dng bng ti nguyn nh hnh. Tc l to ra 1 chui vi tn l hello. Sau nh ngha ni dung ca hello trong file strings.xml trong th mc values. 2. M file strings.xml trong th mc values v sa nh sau:

Chy chng trnh v bn s hiu main.xml dng ti nguyn t strings.xml nh th no. nguyendoanhkhoa@yahoo.com Page 1

3. M file main.xml , sa ni dung nm trong LinearLayout nh sau: V ta s c giao din nh sau:

Cc thuc tnh mi: Android:id : t id cho i tng. Bt u phi l @+id/ ri n mt ci tn (phn lp trnh s dng ci tn nh x n i tng) Chiu rng, cao ca layout c th dng n v tnh nh dp,px, in, mm,pt, sp (dp n v tru tng da vo mt vt l ca mng hnh, 1dp tng ng 1px trn mng hnh 160dpi) Android:layout_gravity: canh n (trn,di) so vi cha n. Android:gravity: canh (trn di) ni dung bn trong n. 4. Tip theo ta bt u lp trnh. Double Click vo file CongHaiSoActivity.java trong th mc myproject1/src/com.android.myproject1.

nguyendoanhkhoa@yahoo.com

Page 2

5. B sung thm cc phn m lnh nh sau:

2 3

Phn 1: Nhng th vin cn thit cho cc i tng. Phn 2: To trc cc bin, i tng. Phn 3: nh x t giao din vo cc i tng.

nguyendoanhkhoa@yahoo.com

Page 3

6. Pht trin tip bng cch bt s kin Click cho 2 nt. Ch : khi ta g tendoituong ri du . th eclipse s h tr bng cch hin ra hp thoi lit k cc thuc tnh, phng thc ca i tng , hoc ta c th dng Ctrl+Space hin, hy tn dng n vit m nhanh hn nh sau (xem hnh) g n View. hp thoi hin ra hy chn vo View.OnClickListener() hon tt cu lnh:

Cu lnh sau khi hon tt s nh sau:

Ch : Bn thy m pht sinh vn cn thiu 1 du ; nh b sung thm. nguyendoanhkhoa@yahoo.com Page 4

Bn di phn ghi chu //TODO Auto-generated method stub l nhng g s lm khi nt btnTang c nhn. 7. Bn lm tng t cho nt btnGiam v vit lnh bn trong 2 nt tng/gim bin m v dng hm setText gn kt qu cho textview. M lnh cui cng nh sau:

8. n y bn c th chy chng trnh thy kt qu. 9. Ch , bn thy rng chui mc nh ca chng trnh l tong 2 so la khng ng vi chng trnh, bn c th d dng sa n trong file string.xml. Tn Activity l CongHaiSoActivity cng khng ng. Bn c th sa tn Activity nh sau: Click phi file .java/Refactor/Rename:

nguyendoanhkhoa@yahoo.com

Page 5

Trong hp thoi Rename Compilation Unit g vo tn mi: TangGiamMotSoActivity, check 3 du chn u tin Next/Next/Finish

nguyendoanhkhoa@yahoo.com

Page 6