Home | History | Annotate | Download | only in ui
      1 page.title=S kin Nhp liu
      2 parent.title=Giao din Ngi dng
      3 parent.link=index.html
      4 @jd:body
      5 
      6 <div id="qv-wrapper">
      7 <div id="qv">
      8   <h2>Trong ti liu ny</h2>
      9   <ol>
     10     <li><a href="#EventListeners">i tng theo di S kin</a></li>
     11     <li><a href="#EventHandlers">B x l S kin</a></li>
     12     <li><a href="#TouchMode">Ch  Cm ng</a></li>
     13     <li><a href="#HandlingFocus">X l Tiu im</a></li>
     14   </ol>
     15 
     16 </div>
     17 </div>
     18 
     19 <p>Trn Android, c nhiu cch  can thip vo cc s kin t tng tc ca mt ngi dng vi ng dng ca bn.
     20 Khi xem xt cc s kin trong giao din ngi dng ca bn, cch tip cn l chp li s kin t
     21 i tng Dng xem c th m ngi dng tng tc vi. Lp Dng xem s cung cp phng thc  lm vic ny.</p>
     22 
     23 <p>Trong cc lp Dng xem khc nhau m bn s s dng  son b tr ca mnh, bn c th thy mt vi phng php gi li
     24 cng khai dng nh hu ch i vi s kin UI. Nhng phng php ny c khun kh Android gi khi
     25 xy ra hnh ng tng ng trn i tng . V d, khi mt Dng xem (chng hn nh mt Nt) c chm vo,
     26 phng php <code>onTouchEvent()</code> c gi trn i tng . Tuy nhin,  can thip vo iu ny, bn phi m rng
     27 lp v khng ch phng php . Tuy nhin, vic m rng mi i tng Dng xem
     28  x l mt s kin nh vy s l khng thc t. y l l do ti sao lp Dng xem cng cha
     29 mt tp hp giao din lng nhau cng cc phng php gi li m bn c th nh ngha d dng hn nhiu. Nhng giao din ny,
     30 c gi l <a href="#EventListeners">i tng theo di s kin</a>, l tm v  bn chp li tng tc gia ngi dng vi UI ca bn.</p>
     31 
     32 <p>Trong khi cc i tng theo di s kin s thng c s dng  theo di tng tc ca ngi dng, c th
     33 c lc bn mun m rng mt lp Dng xem  xy dng mt thnh phn ty chnh.
     34 C th l bn mun m rng lp {@link android.widget.Button}
     35  khin ci g  trng n tng hn. Trong trng hp ny, bn s c th nh ngha cc hnh vi s kin mc nh cho lp
     36 ca mnh bng cch s dng <a href="#EventHandlers">b x l s kin</a> ca lp.</p>
     37 
     38 
     39 <h2 id="EventListeners">i tng theo di S kin</h2>
     40 
     41 <p>i tng theo di s kin l mt giao din trong lp {@link android.view.View} cha mt phng php gi li
     42 n l. Nhng phng php ny s c khun kh Android gi khi Dng xem m i tng theo di 
     43 c ng k vi b kch khi bi tng tc gia ngi dng vi mc trong UI.</p>
     44 
     45 <p>Trong giao din ca i tng theo di s kin l nhng phng php gi li sau:</p>
     46 
     47 <dl>
     48   <dt><code>onClick()</code></dt>
     49     <dd>T {@link android.view.View.OnClickListener}.
     50     Phng php ny c gi khi ngi dng chm vo mc
     51     (khi  ch  cm ng), hoc ly tiu im vo mt mc bng phm iu hng hoc bi xoay v
     52     nhn phm "enter" ph hp hoc nhn bi xoay.</dd>
     53   <dt><code>onLongClick()</code></dt>
     54     <dd>T {@link android.view.View.OnLongClickListener}.
     55     Phng php ny c gi khi ngi gi chm v gi mc (khi  ch  cm ng), hoc
     56     ly tiu im vo mt mc bng phm iu hng hoc bi xoay v
     57     nhn v gi phm "enter" ph hp hoc nhn v gi bi xoay (trong mt giy).</dd>
     58   <dt><code>onFocusChange()</code></dt>
     59     <dd>T {@link android.view.View.OnFocusChangeListener}.
     60     Phng php ny c gi khi ngi dng iu hng ln hoc ra khi mt mc bng cch s dng cc phm iu hng hoc bi xoay.</dd>
     61   <dt><code>onKey()</code></dt>
     62     <dd>T {@link android.view.View.OnKeyListener}.
     63     Phng php ny c gi khi ngi dng c ly tiu im vo mt mc v nhn hoc nh phm cng trn thit b.</dd>
     64   <dt><code>onTouch()</code></dt>
     65     <dd>T {@link android.view.View.OnTouchListener}.
     66     Phng php ny c gi khi ngi dng thc hin mt hnh ng c coi nh mt s kin chm, bao gm nhn, nh,
     67     hoc bt k ng tc chuyn ng no trn mn hnh (trong ng bin ca mc ).</dd>
     68   <dt><code>onCreateContextMenu()</code></dt>
     69     <dd>T {@link android.view.View.OnCreateContextMenuListener}.
     70     Phng php ny c gi khi mt Menu Ng cnh ang c xy dng (kt qu ca mt s kin "nhp gi" ko di). Xem phn tho lun v
     71     menu ng cnh trong hng dn dnh cho nh pht trin <a href="{@docRoot}guide/topics/ui/menus.html#context-menu">Menu</a>
     72 .</dd>
     73 </dl>
     74 
     75 <p>Nhng phng php ny l phng php duy nht nm trong giao din tng ng ca chng.  nh ngha mt trong nhng phng php ny
     76 v x l s kin ca bn, hy trin khai giao din lng nhau trong Hot ng ca bn hoc nh ngha n thnh mt lp v danh.
     77 Sau , chuyn mt thc th trin khai ca bn
     78 ti phng php <code>View.set...Listener()</code> tng ng. (V d, gi
     79 <code>{@link android.view.View#setOnClickListener(View.OnClickListener) setOnClickListener()}</code>
     80 v chuyn cho n trin khai {@link android.view.View.OnClickListener OnClickListener} ca bn.)</p>
     81 
     82 <p>V d bn di cho bit cch ng k mt i tng theo di khi nhp cho mt Nt. </p>
     83 
     84 <pre>
     85 // Create an anonymous implementation of OnClickListener
     86 private OnClickListener mCorkyListener = new OnClickListener() {
     87     public void onClick(View v) {
     88       // do something when the button is clicked
     89     }
     90 };
     91 
     92 protected void onCreate(Bundle savedValues) {
     93     ...
     94     // Capture our button from layout
     95     Button button = (Button)findViewById(R.id.corky);
     96     // Register the onClick listener with the implementation above
     97     button.setOnClickListener(mCorkyListener);
     98     ...
     99 }
    100 </pre>
    101 
    102 <p>Bn cng c th thy tin hn khi trin khai OnClickListener nh mt phn trong Hot ng ca mnh.
    103 Lm vy s trnh phi ti lp b sung v phn b i tng. V d:</p>
    104 <pre>
    105 public class ExampleActivity extends Activity implements OnClickListener {
    106     protected void onCreate(Bundle savedValues) {
    107         ...
    108         Button button = (Button)findViewById(R.id.corky);
    109         button.setOnClickListener(this);
    110     }
    111 
    112     // Implement the OnClickListener callback
    113     public void onClick(View v) {
    114       // do something when the button is clicked
    115     }
    116     ...
    117 }
    118 </pre>
    119 
    120 <p>Lu  rng phng php gi li <code>onClick()</code> trong v d trn khng c gi tr
    121 tr v, nhng mt s phng php i tng theo di s kin khc phi tr v mt boolean. L do
    122 ny ph thuc vo s kin. Vi s t s kin thc hin nh vy, sau y l l do:</p>
    123 <ul>
    124   <li><code>{@link android.view.View.OnLongClickListener#onLongClick(View) onLongClick()}</code> -
    125     Tr v mt boolean cho bit bn  x l s kin v s kin khng nn c tip tc hay khng.
    126     C th, tr v <em>true</em>  cho bit rng bn  x l s kin v n nn dng  y;
    127     tr v <em>false</em> nu bn cha x l n v/hoc s kin s tip tc i vi bt k
    128     i tng theo di khi nhp no khc.</li>
    129   <li><code>{@link android.view.View.OnKeyListener#onKey(View,int,KeyEvent) onKey()}</code> -
    130     Tr v mt boolean cho bit bn  x l s kin v s kin khng nn c tip tc hay khng.
    131     C th, tr v <em>true</em> s cho bit rng bn  x l s kin v n nn dng  y;
    132     tr v <em>false</em> nu bn cha x l n v/hoc s kin s tip tc i vi bt k
    133     i tng theo di trn phm no khc.</li>
    134   <li><code>{@link android.view.View.OnTouchListener#onTouch(View,MotionEvent) onTouch()}</code> -
    135     Tr v mt boolean cho bit i tng theo di ca bn c x l s kin ny hay khng. iu quan trng  l
    136     s kin ny c th c nhiu hnh ng ni tip nhau. V vy, nu bn tr v <em>false</em> khi
    137     nhn c s kin hnh ng hng xung, bn s cho bit rng mnh cha x l s kin v cng
    138     khng quan tm ti cc hnh ng sau  t s kin ny. V th, bn s khng b gi v bt k hnh ng no khc
    139     trong s kin, chng hn nh mt c ch ngn tay, hay s kin hnh ng cho s kin hng ln.</li>
    140 </ul>
    141 
    142 <p>Ghi nh rng cc s kin phm cng lun c chuyn ti Dng xem ang c ly tiu im. Chng c chuyn bt u t trn cng
    143 ca phn cp Dng xem, ri xung di, ti khi chng n ch ph hp. Nu Dng xem ca bn (hoc con ca Dng xem)
    144 hin c tiu im, khi  bn c th thy hnh trnh ca s kin qua phng php <code>{@link android.view.View#dispatchKeyEvent(KeyEvent)
    145 dispatchKeyEvent()}</code>. Mt cch khc  chp li cc s kin phm bm thng qua Dng xem ca mnh, bn cng c th nhn
    146 tt c s kin bn trong Hot ng ca mnh bng <code>{@link android.app.Activity#onKeyDown(int,KeyEvent) onKeyDown()}</code>
    147 v <code>{@link android.app.Activity#onKeyUp(int,KeyEvent) onKeyUp()}</code>.</p>
    148 
    149 <p>ng thi, khi ngh ti nhp liu vn bn cho ng dng ca bn, hy nh rng nhiu thit b ch c cc phng php
    150 nhp liu mm. Nhng phng php nh vy khng bt buc phi da trn phm bm; mt s c th s dng nhp liu bng ging ni, vit tay, v.v. Ngay c khi
    151 mt phng php nhp liu trnh by mt giao din nh bn phm, n s thng <strong>khng</strong> kch khi h s kin
    152 <code>{@link android.app.Activity#onKeyDown(int,KeyEvent) onKeyDown()}</code>. Bn khng nn
    153 xy dng UI yu cu kim sot cc thao tc nhn phm c th tr khi mun gii hn ng dng ca bn  mt s thit b
    154 c bn phm cng. C th, khng c da vo nhng phng php ny  xc thc nhp liu khi ngi dng nhn phm
    155 quay li; thay vo , hy s dng cc hnh ng nh {@link android.view.inputmethod.EditorInfo#IME_ACTION_DONE}  bo hiu vi
    156 phng php nhp liu bn k vng ng dng ca mnh s phn ng nh th no  n c th thay i UI ca mnh cho c ngha. Trnh cc gi nh
    157 v cch thc hot ng ca mt phng php nhp liu mm v ch tin tng  n cung cp vn bn  c nh dng cho ng dng ca mnh.</p>
    158 
    159 <p class="note"><strong>Lu :</strong> Android s gi b x l s kin trc ri mi ti b x l
    160 mc nh ph hp t nh ngha lp. Nh th, vic tr v <em>true</em> t nhng i tng theo di s kin ny s dng
    161 vic lan truyn s kin ti i tng theo di s kin khc v cng s chn phng php gi li ti
    162 b x l s kin mc nh trong Dng xem. V th, hy chc chn rng bn mun chm dt s kin khi tr v <em>true</em>.</p>
    163 
    164 
    165 <h2 id="EventHandlers">B x l S kin</h2>
    166 
    167 <p>Nu bn ang xy dng mt thnh phn ty chnh t Dng xem, khi  bn s c th nh ngha mt vi phng php gi li
    168 c s dng nh b x l s kin mc nh.
    169 Trong ti liu v <a href="{@docRoot}guide/topics/ui/custom-components.html">Thnh phn
    170 Ty chnh</a>, bn s tm hiu v mt s phng php gi li ph bin c s dng  x l s kin,
    171 bao gm:</p>
    172 <ul>
    173   <li><code>{@link  android.view.View#onKeyDown}</code> - c gi khi xy ra mt s kin phm bm mi.</li>
    174   <li><code>{@link  android.view.View#onKeyUp}</code> - c gi khi xy ra mt s kin phm bm hng ln.</li>
    175   <li><code>{@link  android.view.View#onTrackballEvent}</code> - c gi khi xy ra mt s kin chuyn ng bi xoay.</li>
    176   <li><code>{@link  android.view.View#onTouchEvent}</code> - c gi khi xy ra mt s kin chuyn ng mn hnh cm ng.</li>
    177   <li><code>{@link  android.view.View#onFocusChanged}</code> - c gi khi dng xem c hoc mt tiu im.</li>
    178 </ul>
    179 <p>C mt s phng php khc m bn cn lu , chng khng thuc lp Dng xem,
    180 nhng c th tc ng trc tip ti cch bn c th x l s kin. V th, khi qun l cc s kin phc tp hn bn trong
    181 mt b tr, hy xt nhng phng php khc sau:</p>
    182 <ul>
    183   <li><code>{@link  android.app.Activity#dispatchTouchEvent(MotionEvent)
    184     Activity.dispatchTouchEvent(MotionEvent)}</code> - Phng php ny cho php {@link
    185     android.app.Activity} ca bn can thip vo tt c s kin chm trc khi chng c phn phi ti ca s.</li>
    186   <li><code>{@link  android.view.ViewGroup#onInterceptTouchEvent(MotionEvent)
    187     ViewGroup.onInterceptTouchEvent(MotionEvent)}</code> - Phng php ny cho php mt {@link
    188     android.view.ViewGroup} xem s kin khi chng c phn phi ti Dng xem con.</li>
    189   <li><code>{@link  android.view.ViewParent#requestDisallowInterceptTouchEvent(boolean)
    190     ViewParent.requestDisallowInterceptTouchEvent(boolean)}</code> - Gi phng php
    191     ny trn Dng xem m  cho bit rng n s khng can thip vo cc s kin chm bng <code>{@link
    192     android.view.ViewGroup#onInterceptTouchEvent(MotionEvent)}</code>.</li>
    193 </ul>
    194 
    195 <h2 id="TouchMode">Ch  Cm ng</h2>
    196 <p>
    197 Khi mt ngi dng ang iu hng trong mt giao din ngi dng bng phm hng hoc bi xoay, cn
    198 ly tiu im ti cc mc c th hnh ng (nh nt) sao cho ngi dng c th thy
    199 mc no s chp nhn nhp liu.  Tuy nhin, nu thit b c kh nng cm ng, v ngi dng
    200 bt u tng tc vi giao din bng cch chm vo n, khi  khng cn cn
    201 t sng mc hay ly tiu im ti mt Dng xem c th na.  Do , c mt ch  cho
    202 tng tc c tn l "ch  cm ng."
    203 </p>
    204 <p>
    205 i vi thit b c kh nng cm ng, sau khi ngi dng chm vo mn hnh, thit b
    206 s vo ch  cm ng.  T im ny tr i, ch nhng Dng xem m
    207 {@link android.view.View#isFocusableInTouchMode} l ng mi c th ly tiu im, chng hn nh ch  xem cc widget chnh sa vn bn.
    208 Cc Dng xem chm c, chng hn nh nt, s khng ly c tiu im khi chm; chng s ch n gin
    209 khi chy i tng theo di khi nhp ca mnh khi c nhn.
    210 </p>
    211 <p>
    212 Bt c khi no mt ngi dng nhn phm hng hoc cun bng bi xoay, thit b s
    213 thot ch  cm ng, v tm mt dng xem  ly tiu im. Lc ny, ngi dng c th tip tc tng tc
    214 vi giao din ngi dng m khng chm vo mn hnh.
    215 </p>
    216 <p>
    217 Trng thi ch  cm ng s c duy tr trn ton b h thng (tt c ca s v hot ng).
    218  truy vn trng thi hin ti, bn c th gi
    219 {@link android.view.View#isInTouchMode}  xem liu thit b c ang  trong ch  cm ng hay khng.
    220 </p>
    221 
    222 
    223 <h2 id="HandlingFocus">X l Tiu im</h2>
    224 
    225 <p>Khun kh s x l chuyn ng ca tiu im thng xuyn hi p li nhp liu ca ngi dng.
    226 Vic ny bao gm thay i tiu im khi Dng xem b loi b hoc n i, hoc khi Dng xem
    227 mi c sn. Dng xem th hin s sn sng ly tiu im ca chng
    228 thng qua phng php <code>{@link android.view.View#isFocusable()}</code>.  thay i vic liu mt Dng xem c th ly
    229 tiu im hay khng, hy gi <code>{@link android.view.View#setFocusable(boolean) setFocusable()}</code>.  Khi  trong ch  cm ng,
    230 bn c th truy vn xem Dng xem c cho php ly tiu im bng <code>{@link android.view.View#isFocusableInTouchMode()}</code> hay khng.
    231 Bn c th thay i iu ny bng <code>{@link android.view.View#setFocusableInTouchMode(boolean) setFocusableInTouchMode()}</code>.
    232 </p>
    233 
    234 <p>Chuyn ng tiu im c da trn mt gii thut tm kim i tng gn nht theo
    235 mt hng cho trc. Trong cc trng hp him gp, gii thut mc nh c th khng khp vi
    236 hnh vi theo  nh ca nh pht trin. Trong nhng tnh hung ny, bn c th cung cp
    237 cc khng ch r rng vi cc thuc tnh XML sau trong tp b tr:
    238 <var>nextFocusDown</var>, <var>nextFocusLeft</var>, <var>nextFocusRight</var>, v
    239 <var>nextFocusUp</var>. Thm mt trong nhng thuc tnh ny vo Dng xem m <em>t</em> 
    240 tiu im ang ri khi. nh ngha gi tr ca thuc tnh l id ca Dng xem
    241 <em>m</em> cn c ly tiu im. V d:</p>
    242 <pre>
    243 &lt;LinearLayout
    244     android:orientation="vertical"
    245     ... >
    246   &lt;Button android:id="@+id/top"
    247           android:nextFocusUp="@+id/bottom"
    248           ... />
    249   &lt;Button android:id="@+id/bottom"
    250           android:nextFocusDown="@+id/top"
    251           ... />
    252 &lt;/LinearLayout>
    253 </pre>
    254 
    255 <p>Thng thng, trong b tr thng ng ny, vic iu hng ln t Nt u tin s khng
    256 i ti u ht v vic iu hng xung t Nt th hai cng vy. Gi th khi Nt trn cng
    257  nh ngha Nt di cng l <var>nextFocusUp</var> (v ngc li), tiu im iu hng s
    258 lun chuyn t trn-xung-di v di-ln-trn.</p>
    259 
    260 <p>Nu bn mun khai bo mt Dng xem l c th ly tiu im trong UI ca mnh (thng thng th khng),
    261 hy thm thuc tnh XML <code>android:focusable</code> vo Dng xem, trong khai bo b tr ca bn.
    262 t gi tr <var>true</var>. Bn cng c th khai bo mt Dng xem
    263 l c th ly tiu im trong khi  Ch  Cm ng bng <code>android:focusableInTouchMode</code>.</p>
    264 <p> yu cu mt Dng xem c th  ly tiu im, hy gi <code>{@link android.view.View#requestFocus()}</code>.</p>
    265 <p> theo di cc s kin tiu im (c thng bo khi mt Dng xem nhn c hoc mt tiu im), hy s dng
    266 <code>{@link android.view.View.OnFocusChangeListener#onFocusChange(View,boolean) onFocusChange()}</code>,
    267 nh c  cp trong phn <a href="#EventListeners">i tng theo di S kin</a> bn trn.</p>
    268 
    269 
    270 
    271 <!--
    272 <h2 is="EventCycle">Event Cycle</h2>
    273    <p>The basic cycle of a View is as follows:</p>
    274    <ol>
    275     <li>An event comes in and is dispatched to the appropriate View. The View
    276     handles the event and notifies any listeners.</li>
    277     <li>If, in the course of processing the event, the View's bounds may need
    278     to be changed, the View will call {@link android.view.View#requestLayout()}.</li>
    279     <li>Similarly, if in the course of processing the event the View's appearance
    280     may need to be changed, the View will call {@link android.view.View#invalidate()}.</li>
    281     <li>If either {@link android.view.View#requestLayout()} or {@link android.view.View#invalidate()} were called,
    282     the framework will take care of measuring, laying out, and drawing the tree
    283     as appropriate.</li>
    284    </ol>
    285 
    286    <p class="note"><strong>Note:</strong> The entire View tree is single threaded. You must always be on
    287    the UI thread when calling any method on any View.
    288    If you are doing work on other threads and want to update the state of a View
    289    from that thread, you should use a {@link android.os.Handler}.
    290    </p>
    291 -->
    292