Professional Documents
Culture Documents
Lap Trinh Xu Ly Cu Chi Trong Android PDF
Lap Trinh Xu Ly Cu Chi Trong Android PDF
<TextView
android:id="@+id/word_study_part_of_speech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:id="@+id/word_study_definition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:textColor="@color/black"
android:textSize="18sp" />
<LinearLayout
android:id="@+id/widget62"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<Button
android:id="@+id/next_word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/next_word" />
<Button
android:id="@+id/take_quiz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:text="@string/take_quiz" />
</LinearLayout>
</LinearLayout>
Nh bn thy XML trong Lit k 1, mi widget c mt s thuc tnh nh hng
trc tip ln n. V d, mt button (nt bm) trong 2 button c mt phn t text m
chui ca n tr ti resource element (phn t ti nguyn). Resource element c
nh ngha trong mt tp resourcec tn l strings.xml, c cha trong th
mc res/values. Lit k 2 hin th tp strings.xml.
Lit k 2. tp resource nh ngha mu v nhn ca button
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Overheard Word</string>
<color name="white">#ffffff</color>
<color name="black">#000000</color>
<string name="next_word">Next word</string>
<string name="take_quiz">Take quiz</string>
</resources>
Nu bn mun bit giao din ng dng ca bn trng nh th no, bn c th bt
u vi trnh m phng. Ti nhn vo th Graphical Layout trong Eclipse,
nh Hnh 7.
Hnh 7. ng dng Overheard Word chy trn Eclipse
Placeholder text
Khi xem giao din ngi dng, ti hay nhm ln khong trng i vi thuc tnh
word. Nhng khng sao: ti s thm mt vi on vn bn mu cho nhiu tng
thc t rng on vn bn s hin th nh th no trc khi ti vit m lnh..
u tin, ti thm mt thuc tnh android:text cho mi widget TextView, ging
nh ti lm vi cc nhn ca button. By gi ti thm cc placeholder text nh
sau:
Lit k 3. To cc placeholders vi android:text
<TextView
android:id="@+id/word_study_part_of_speech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textColor="@color/black"
android:textSize="18sp"
android:text="Part of Speech"/>
Nh bn c th thy trong Lit k 3, ti thm mt on vn bn mu ("Part of
Speech") vo TextViewword_study_part_of_speech. Ti cp nht mt s kiu
phn t bng cch tng khch thc ca vn bn, nh ngha mu vn bn v canh
gia widget trong layout. Cc thuc tnh trong Lit k 3 l ph bin nh ngha
}
public boolean isSwipeRight(MotionEvent e1, MotionEvent e2, float velocityX)
{
return isSwipe(e2.getX(), e1.getX(), velocityX);
}
private boolean isSwipeDistance(float coordinateA, float coordinateB) {
return (coordinateA - coordinateB) > this.swipe_distance;
}
private boolean isSwipeSpeed(float velocity) {
return Math.abs(velocity) > this.swipe_velocity;
}
private boolean isSwipe(float coordinateA, float coordinateB, float velocity) {
return isSwipeDistance(coordinateA, coordinateB)
&& isSwipeSpeed(velocity);
}
}
By gi ti c mt lp tin dng cho ti bit c liu c ch c xy ra hay khng,
v ti c th a n vo giao din. Nh li khi ln u ti to mt Activity mc
nh, tn l OverheardWord. Ti b tt c m mc nh m Eclipse sinh ra, v
thm vo mt s th. lng nghe v phn hi li hnh ng lt tay, ti nh
ngha mt GestureDetector cho Android, n nhm thc hin s
kin OnGestureListener. Rt may, Android to ra mt lp tin li m ti c th
thc hin thng qua mt lp n danh tn l SimpleOnGestureListener. Sau ti
ghi phng thc onFling.
Nhn dng hnh ng lt tay trong giao din ngi dng
Trong phng thc onCreate ca Activity, ti bt u to mt GestureDetector vi
mt SimpleOnGestureListener, c dng trong lp SwipeDetector.
Tip theo, ti vit mt hp thoi nh ch ra rng s kin c ging ln khi c
ai lt tay qua. y l loi hp thoi nh, vng i ngn, trn Android gi
l Toast. Trong mt vi trng hp, mt Toast c th bao gm ch mt dng m.
Lit k 5. GestureDetector
private GestureDetector initGestureDetector() {
return new GestureDetector(new SimpleOnGestureListener() {
gestureDetector = initGestureDetector();
View view = findViewById(R.id.LinearLayout1);
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
view.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
}
Trong phng thc onCreate, ti qun l th hin ca View thng qua mt Id.
Hu ht cc nh pht trin Java nh gi cao s tin dng Android-ism ny.
Tm view theo phng php ID
Hy nh khi bn nh ngha XML cho giao din, cng vi cc gi tr tng ng
trong tp tin ti nguyn khc nhau (nh strings.xml)? Khi ti bin dch ng dng
Android, m XML chuyn thnh m t sinh v gi Id tng ng vo mt lp
gi l R. Hy kim tra lp R trong th mc gen thuc d n ca bn trc khi tip
tc.
Xem xt tp layout XML ca bn (bn c th nh rng ti t tn
l activity_overheard_word.xml). Trong tp XML , mi widget u c thuc
tnh id. V d, id ca layout trong ng dng ca ti
l android:id="@+id/LinearLayout1", hay ngn hn lLinearLayout1. Tn Id c
Eclipse sinh ra t ng nhng ti c th thay i bt k lc no. iu quan trng
l id, LinearLayout1, c thuc tnh tng ng vi lp R, nh trong Lit k 7.
Lit k 7. Widget IDs trong lp R
public static final class id {
public static final int LinearLayout1=0x7f090000;
public static final int action_settings=0x7f090004;
public static final int word_study_definition=0x7f090003;
public static final int word_study_part_of_speech=0x7f090002;
public static final int word_study_word=0x7f090001;
}