Home | History | Annotate | Download | only in ui
      1 page.title=Menu
      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="#xml">nh ngha mt Menu trong XML</a></li>
     11   <li><a href="#options-menu">To mt Menu Ty chn</a>
     12     <ol>
     13       <li><a href="#RespondingOptionsMenu">X l s kin nhp</a></li>
     14       <li><a href="#ChangingTheMenu">Thay i cc mc menu vo thi gian chy</a></li>
     15     </ol>
     16   </li>
     17   <li><a href="#context-menu">To mt Menu Ng cnh</a>
     18     <ol>
     19       <li><a href="#FloatingContextMenu">To mt menu ng cnh ni</a></li>
     20       <li><a href="#CAB">S dng ch  hnh ng theo ng cnh</a></li>
     21     </ol>
     22   </li>
     23   <li><a href="#PopupMenu">To mt Menu Bt ln</a>
     24     <ol>
     25       <li><a href="#PopupEvents">X l s kin nhp</a></li>
     26     </ol>
     27   </li>
     28   <li><a href="#groups">To Nhm Menu</a>
     29     <ol>
     30       <li><a href="#checkable">S dng mc menu c th chn</a></li>
     31     </ol>
     32   </li>
     33   <li><a href="#intents">Thm Mc Menu da trn  nh</a>
     34     <ol>
     35       <li><a href="#AllowingToAdd">Cho php hot ng ca bn c thm vo cc menu khc</a></li>
     36     </ol>
     37   </li>
     38 </ol>
     39 
     40   <h2>Lp kha</h2>
     41   <ol>
     42     <li>{@link android.view.Menu}</li>
     43     <li>{@link android.view.MenuItem}</li>
     44     <li>{@link android.view.ContextMenu}</li>
     45     <li>{@link android.view.ActionMode}</li>
     46   </ol>
     47 
     48   <h2>Xem thm</h2>
     49   <ol>
     50     <li><a href="{@docRoot}guide/topics/ui/actionbar.html">Thanh Hnh ng</a></li>
     51     <li><a href="{@docRoot}guide/topics/resources/menu-resource.html">Ti nguyn Menu</a></li>
     52     <li><a href="http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html">Ni
     53 Tm bit vi Nt Menu</a></li>
     54   </ol>
     55 </div>
     56 </div>
     57 
     58 <p>Menu l mt thnh phn giao din ngi dng ph bin trong nhiu loi ng dng.  cung cp mt
     59 tri nghim ngi dng quen thuc v nht qun, bn nn s dng cc API {@link android.view.Menu}  trnh by
     60 hnh ng ngi dng v cc ty chn khc trong hot ng ca mnh.</p>
     61 
     62 <p>Bt u vi Android 3.0 (API mc 11), cc thit b da trn nn tng Android khng cn phi
     63 cung cp mt nt <em>Menu</em> chuyn dng na. Vi s thay i ny, cc ng dng Android cn trnh khi
     64 s ph thuc vo bng iu khin menu 6 mc truyn thng ny m thay vo  cung cp mt thanh hnh ng  trnh by
     65 cc hnh ng ngi dng thng dng.</p>
     66 
     67 <p>Mc d thit k v tri nghim ngi dng i vi mt s mc menu  thay i, ng ngha  nh ngha
     68 tp hp hnh ng v ty chn th vn da trn cc API {@link android.view.Menu}. Hng dn
     69 ny trnh by cch to ba loi menu hay trnh by hnh ng c bn trn tt c
     70 phin bn Android:</p>
     71 
     72 <dl>
     73   <dt><strong>Menu ty chn v thanh hnh ng</strong></dt>
     74     <dd><a href="#options-menu">Menu ty chn</a> l tp hp cc mc menu c bn cho mt
     75 hot ng.  l ni bn nn t cc hnh ng c tc ng chung ti ng dng, chng hn nh
     76 "Tm kim," "Son e-mail" v "Ci t."
     77   <p>Nu bn ang pht trin cho phin bn Android 2.3 hoc thp hn, ngi dng c th
     78 hin bng iu khin menu ty chn bng cch nhn nt <em>Menu</em>.</p>
     79   <p>Trn phin bn Android 3.0 tr ln, cc mc t menu ty chn c trnh by bi <a href="{@docRoot}guide/topics/ui/actionbar.html">thanh hnh ng</a>, l s kt hp gia cc mc hnh ng
     80 trn mn hnh v cc ty chn trn. Bt u vi phin bn Android 3.0, nt <em>Menu</em> b b i (mt s
     81 thit b
     82 khng c), v th bn nn chuyn sang s dng thanh hnh ng  cho php truy cp vo hnh ng v
     83 cc ty chn khc.</p>
     84   <p>Xem phn v <a href="#options-menu">To mt Menu Ty chn</a>.</p>
     85     </dd>
     86     
     87   <dt><strong>Menu ng cnh v ch  hnh ng theo ng cnh</strong></dt>
     88   
     89    <dd>Menu ng cnh l mt <a href="#FloatingContextMenu">menu ni</a> xut hin khi
     90 ngi dng thc hin nhp gi trn mt phn t. N cung cp cc hnh ng nh hng ti ni dung hoc
     91 khung ng cnh c chn.
     92   <p>Khi pht trin cho phin bn Android 3.0 tr ln, thay vo , bn nn s dng <a href="#CAB">ch  hnh ng theo ng cnh</a>  kch hot cc hnh ng trn ni dung c chn. Ch  ny hin th
     93 cc mc hnh ng nh hng ti ni dung c chn trong mt thanh  trn cng ca mn hnh v cho php ngi dng
     94 chn nhiu mc.</p>
     95   <p>Xem phn ni v <a href="#context-menu">To Menu Ng cnh</a>.</p>
     96 </dd>
     97     
     98   <dt><strong>Menu bt ln</strong></dt>
     99     <dd>Menu bt ln s hin th danh sch cc mc trong mt danh sch thng ng c neo vo dng xem
    100  gi ra menu. Nn cung cp mt phn trn gm cc hnh ng lin quan ti ni dung c th hoc
    101 nhm cung cp cc ty chn cho phn th hai ca mt lnh. Cc hnh ng trong mt menu bt ln
    102 <strong>khng</strong> nn trc tip nh hng ti ni dung tng ng&mdash; l vic ca hnh ng ng cnh
    103 . Thay vo , menu bt ln p dng cho cc hnh ng m rng lin quan ti cc vng ni dung trong hot ng
    104 ca bn.
    105   <p>Xem phn v <a href="#PopupMenu">To mt Menu Bt ln</a>.</p>
    106 </dd>
    107 </dl>
    108 
    109 
    110 
    111 <h2 id="xml">nh ngha mt Menu trong XML</h2>
    112 
    113 <p>i vi tt c cc loi menu, Android cung cp mt nh dng XML chun  nh ngha cc mc menu.
    114 Thay v xy dng mt menu trong m ca hot ng ca bn, bn nn nh ngha mt menu v tt c cc mc ca n trong mt
    115 <a href="{@docRoot}guide/topics/resources/menu-resource.html">ti nguyn menu</a> XML. Khi , bn c th
    116 bung ti nguyn menu (ti n nh mt i tng {@link android.view.Menu}) trong hot ng hoc
    117 phn on ca mnh.</p>
    118 
    119 <p>S dng mt ti nguyn menu l mt cch lm hay v mt vi l do:</p>
    120 <ul>
    121   <li>N d trc quan ha cu trc menu trong XML hn.</li>
    122   <li>N tch ring ni dung cho menu vi m hnh vi ca ng dng ca bn.</li>
    123   <li>N cho php bn to cc cu hnh menu phi sinh cho cc phin bn nn tng,
    124 kch c mn hnh khc nhau v cc cu hnh khc bng cch tn dng khun kh <a href="{@docRoot}guide/topics/resources/index.html">ti nguyn ng dng</a>.</li>
    125 </ul>
    126 
    127 <p> nh ngha menu, hy to mt tp XML bn trong th mc <code>res/menu/</code>
    128 d n ca bn v xy dng menu vi cc phn t sau:</p>
    129 <dl>
    130   <dt><code>&lt;menu></code></dt>
    131     <dd>nh ngha mt {@link android.view.Menu},  l mt b cha cc mc menu. Phn t 
    132 <code>&lt;menu></code> phi l mt nt gc cho tp v c th gi mt hoc nhiu phn t
    133 <code>&lt;item></code> v <code>&lt;group></code>.</dd>
    134 
    135   <dt><code>&lt;item></code></dt>
    136     <dd>To mt {@link android.view.MenuItem}, n biu din mt mc n trong mt menu. Phn t
    137 ny c th cha mt phn t <code>&lt;menu></code> c lng nhau  to mt menu con.</dd>
    138     
    139   <dt><code>&lt;group></code></dt>
    140     <dd>Mt b cha ty chn, v hnh cho cc phn t {@code &lt;item&gt;}. N cho php bn
    141 phn loi cc mc menu sao cho chng chia s cc tnh cht nh trng thi hin hot v kh nng hin th.  bit thm
    142 thng tin, hy xem phn ni v <a href="#groups">To Nhm Menu</a>.</dd>
    143 </dl>
    144 
    145 
    146 <p>Sau y l mt menu v d c tn l <code>game_menu.xml</code>:</p>
    147 <pre>
    148 &lt;?xml version="1.0" encoding="utf-8"?&gt;
    149 &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    150     &lt;item android:id="@+id/new_game"
    151           android:icon="@drawable/ic_new_game"
    152           android:title="@string/new_game"
    153           android:showAsAction="ifRoom"/&gt;
    154     &lt;item android:id="@+id/help"
    155           android:icon="@drawable/ic_help"
    156           android:title="@string/help" /&gt;
    157 &lt;/menu&gt;
    158 </pre>
    159 
    160 <p>Phn t <code>&lt;item></code> h tr mt vi thuc tnh bn c th s dng  nh ngha biu hin bn ngoi
    161 v hnh vi ca mt mc. Cc mc trong menu trn bao gm nhng thuc tnh sau:</p>
    162 
    163 <dl>
    164   <dt>{@code android:id}</dt>
    165     <dd>Mt ID ti nguyn duy nht i vi mc, n cho php ng dng c th nhn ra mc 
    166 khi ngi dng chn n.</dd>
    167   <dt>{@code android:icon}</dt>
    168     <dd>Mt tham chiu ti mt ni dung v c  dng lm biu tng ca mc.</dd>
    169   <dt>{@code android:title}</dt>
    170     <dd>Mt tham chiu ti mt xu  dng lm tiu  ca mc.</dd>
    171   <dt>{@code android:showAsAction}</dt>
    172     <dd>Quy nh thi im v cch thc mc ny nn xut hin nh mt mc hnh ng trong <a href="{@docRoot}guide/topics/ui/actionbar.html">thanh hnh ng</a>.</dd>
    173 </dl>
    174 
    175 <p>y l nhng thuc tnh quan trng nht bn nn s dng, nhng cn nhiu thuc tnh sn c khc.
    176  bit thng tin v tt c thuc tnh c h tr, hy xem ti liu <a href="{@docRoot}guide/topics/resources/menu-resource.html">Ti nguyn Menu</a>.</p>
    177 
    178 <p>Bn c th thm mt menu con vo mt mc trong bt k menu no (ngoi tr menu con) bng cch thm mt phn t {@code &lt;menu&gt;}
    179 lm con ca {@code &lt;item&gt;}. Cc menu con thng hu ch khi ng dng ca bn c nhiu
    180 chc nng m c th c t chc thnh cc ch , nh cc mc trong thanh menu ca mt ng dng PC (Tp,
    181 Chnh sa, Dng xem, v.v.). V d:</p>
    182 
    183 <pre>
    184 &lt;?xml version="1.0" encoding="utf-8"?&gt;
    185 &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    186     &lt;item android:id="@+id/file"
    187           android:title="@string/file" &gt;
    188         &lt;!-- "file" submenu --&gt;
    189         &lt;menu&gt;
    190             &lt;item android:id="@+id/create_new"
    191                   android:title="@string/create_new" /&gt;
    192             &lt;item android:id="@+id/open"
    193                   android:title="@string/open" /&gt;
    194         &lt;/menu&gt;
    195     &lt;/item&gt;
    196 &lt;/menu&gt;
    197 </pre>
    198 
    199 <p> s dng menu trong hot ng ca mnh, bn cn bung ti nguyn menu (chuyn ti nguyn XML
    200 thnh mt i tng c th lp trnh) bng cch s dng {@link android.view.MenuInflater#inflate(int,Menu)
    201 MenuInflater.inflate()}. Trong nhng phn sau, bn s bit cch bung mt menu i vi mi
    202 loi menu.</p>
    203 
    204 
    205 
    206 <h2 id="options-menu">To mt Menu Ty chn</h2>
    207 
    208 <div class="figure" style="width:200px;margin:0">
    209   <img src="{@docRoot}images/options_menu.png" height="333" alt="" />
    210   <p class="img-caption"><strong>Hnh 1.</strong> Cc menu ty chn trong
    211 Trnh duyt, trn Android 2.3.</p>
    212 </div>
    213 
    214 <p>Menu ty chn l ni bn nn a vo hnh ng v cc ty chn khc lin quan ti
    215 ng cnh hot ng hin ti, chng hn nh "Tm kim," "Son e-mail," v "Ci t."</p>
    216 
    217 <p>Ni m cc mc trong menu ty chn ca bn xut hin trn mn hnh s ph thuc vo phin bn m bn
    218 pht trin ng dng ca mnh cho:</p>
    219 
    220 <ul>
    221   <li>Nu bn pht trin ng dng ca mnh cho phin bn <strong>Android 2.3.x (API mc 10) hoc
    222 thp hn</strong>, ni dung ca menu ty chn s xut hin  di cng mn hnh khi ngi dng
    223 nhn nt <em>Menu</em> nh minh ha trong hnh 1. Khi c m, phn hin th u tin l
    224 menu biu tng
    225 vi ti a su mc menu. Nu menu ca bn bao gm nhiu hn su mc, Android s t
    226 mc th su v phn cn li vo mt menu trn m ngi dng c th m bng cch chn
    227 <em>Thm na</em>.</li>
    228 
    229   <li>Nu bn pht trin ng dng ca mnh cho phin bn <strong>Android 3.0 (API mc 11) v
    230 cao hn</strong>, cc mc t menu ty chn sn  trong <a href="{@docRoot}guide/topics/ui/actionbar.html">thanh hnh ng</a>. Theo mc nh, h thng
    231 t tt c cc mc trong phn trn hnh ng m ngi dng c th hin bng biu tng trn hnh ng pha
    232 bn phi ca thanh hnh ng (hoc bng cch nhn nt <em>Menu</em> ca thit b nu c). 
    233 kch hot
    234 truy cp nhanh vo cc hnh ng quan trng, bn c th a ln mt vi mc xut hin trong thanh hnh ng bng cch thm
    235 {@code android:showAsAction="ifRoom"} vo phn t {@code &lt;item&gt;} tng ng (xem hnh
    236 2). <p> bit thm thng tin v cc mc hnh ng v hnh vi khc ca thanh hnh ng, hy xem hng dn <a href="{@docRoot}guide/topics/ui/actionbar.html">Thanh Hnh ng</a>. </p>
    237 <p class="note"><strong>Lu :</strong> Ngay c khi bn <em>khng</em> ang pht trin cho phin bn Android 3.0 hoc
    238 cao hn, bn c th xy dng b tr thanh hnh ng ca chnh mnh cho hiu ng tng t.  xem v d v cch bn c th h tr cc phin bn cao hn
    239 ca Android bng mt thanh hnh ng, hy xem mu <a href="{@docRoot}resources/samples/ActionBarCompat/index.html">Tng thch vi Thanh Hnh ng</a>
    240 .</p>
    241 </li>
    242 </ul>
    243 
    244 <img src="{@docRoot}images/ui/actionbar.png" alt="" />
    245 <p class="img-caption"><strong>Hnh 2.</strong> Thanh hnh ng t ng dng <a href="{@docRoot}resources/samples/HoneycombGallery/index.html">Honeycomb Gallery</a>, hin th
    246 cc tab iu hng v mt mc hnh ng my nh (cng vi nt trn hnh ng).</p>
    247 
    248 <p>Bn c th khai bo cc mc cho menu ty chn t lp con {@link android.app.Activity}
    249 ca bn hoc mt lp con {@link android.app.Fragment}. Nu c hot ng ca bn v (cc) phn on
    250 u khai bo cc mc cho menu ty chn, chng s c kt hp li trong UI. Cc mc ca hot ng xut hin
    251 trc, sau  l cc mc ca tng phn on theo th t phn on c thm vo
    252 hot ng. Nu cn, bn c th sp xp li cc mc menu bng thuc tnh {@code android:orderInCategory}
    253 trong mi {@code &lt;item&gt;} m bn cn di chuyn.</p>
    254 
    255 <p> quy nh menu ty chn cho mt hot ng, hy khng ch {@link
    256 android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} (cc phn on cung cp
    257 phng php gi li {@link android.app.Fragment#onCreateOptionsMenu onCreateOptionsMenu()} ca chnh mnh). Trong
    258 phng php ny, bn c th bung ti nguyn menu ca mnh (<a href="#xml">c nh ngha trong XML</a>) vo {@link
    259 android.view.Menu} c cung cp trong phng php gi li. V d:</p>
    260 
    261 <pre>
    262 &#64;Override
    263 public boolean onCreateOptionsMenu(Menu menu) {
    264     MenuInflater inflater = {@link android.app.Activity#getMenuInflater()};
    265     inflater.inflate(R.menu.game_menu, menu);
    266     return true;
    267 }
    268 </pre>
    269 
    270 <p>Bn cng c th thm cc mc menu bng cch s dng {@link android.view.Menu#add(int,int,int,int)
    271 add()} v truy xut cc mc bng {@link android.view.Menu#findItem findItem()}  xem li
    272 tnh cht ca chng bng cc API {@link android.view.MenuItem}.</p>
    273 
    274 <p>Nu bn pht trin ng dng ca mnh cho phin bn Android 2.3.x v thp hn, h thng gi {@link
    275 android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()}  to menu ty chn
    276 khi ngi dng m menu ln u tin. Nu bn pht trin cho phin bn Android 3.0 vo cao hn, 
    277 h thng s gi {@link android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} khi
    278 bt u hot ng  hin th cc mc cho thanh hnh ng.</p>
    279 
    280 
    281 
    282 <h3 id="RespondingOptionsMenu">X l s kin nhp</h3>
    283 
    284 <p>Khi ngi dng chn mt mc t menu ty chn (bao gm cc mc hnh ng trong thanh hnh ng),
    285 h thng s gi phng php {@link android.app.Activity#onOptionsItemSelected(MenuItem)
    286 onOptionsItemSelected()} ca hot ng ca bn. Phng php ny thng qua {@link android.view.MenuItem} c chn. Bn
    287 c th nhn bit mc bng cch gi {@link android.view.MenuItem#getItemId()}, n tr v ID duy nht
    288 cho mc menu (c nh ngha bi thuc tnh {@code android:id} trong ti nguyn menu hoc bng mt
    289 s nguyn c cp cho phng php {@link android.view.Menu#add(int,int,int,int) add()}). Bn c th khp
    290 ID ny vi cc mc menu  bit  thc hin hnh ng ph hp. V d:</p>
    291 
    292 <pre>
    293 &#64;Override
    294 public boolean onOptionsItemSelected(MenuItem item) {
    295     // Handle item selection
    296     switch (item.getItemId()) {
    297         case R.id.new_game:
    298             newGame();
    299             return true;
    300         case R.id.help:
    301             showHelp();
    302             return true;
    303         default:
    304             return super.onOptionsItemSelected(item);
    305     }
    306 }
    307 </pre>
    308 
    309 <p>Khi bn x l thnh cng mt mc menu, tr v {@code true}. Nu khng x l c
    310 mc menu, bn nn gi trin khai siu lp ca {@link
    311 android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} (trin khai
    312 mc nh tr v sai).</p>
    313 
    314 <p>Nu hot ng ca bn bao gm cc phn on, trc tin h thng s gi {@link
    315 android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} cho hot ng, ri mi
    316 cho tng phn on (theo th t thm phn on) ti khi tr v
    317 {@code true} hoc tt c phn on u c gi.</p>
    318 
    319 <p class="note"><strong>Mo:</strong> Android 3.0 thm kh nng cho php bn nh ngha hnh vi
    320 khi nhp i vi mt mc menu trong XML, bng cch s dng thuc tnh {@code android:onClick}. Gi tr cho
    321 thuc tnh phi l tn ca mt phng php c nh ngha bi hot ng s dng menu. Phng php
    322 phi cng khai v chp nhn mt tham s {@link android.view.MenuItem} n&mdash;khi h thng
    323 gi phng php ny, n thng qua mc menu c chn.  bit thm thng tin v v d, hy xem ti liu <a href="{@docRoot}guide/topics/resources/menu-resource.html">Ti nguyn Menu</a>.</p>
    324 
    325 <p class="note"><strong>Mo:</strong> Nu ng dng ca bn cha nhiu hot ng v
    326 mt s chng cung cp menu ty chn tng t, hy xem xt to
    327  mt hot ng ch trin khai cc phng php {@link android.app.Activity#onCreateOptionsMenu(Menu)
    328 onCreateOptionsMenu()} v {@link android.app.Activity#onOptionsItemSelected(MenuItem)
    329 onOptionsItemSelected()}. Sau , m rng lp ny i vi mi hot ng cn chia s
    330 menu ty chn tng t. Bng cch ny, bn c th qun l mt b m  x l cc hnh ng
    331 menu v tng lp hu du k tha cc hnh vi menu.
    332 Nu bn mun thm cc mc menu vo mt trong cc hot ng hu du,
    333 hy khng ch {@link android.app.Activity#onCreateOptionsMenu(Menu)
    334 onCreateOptionsMenu()} trong hot ng . Gi {@code super.onCreateOptionsMenu(menu)} sao cho
    335 cc mc menu gc c to, sau  thm cc mc menu mi bng {@link
    336 android.view.Menu#add(int,int,int,int) menu.add()}. Bn cng c th khng ch hnh vi
    337 ca siu lp i vi cc mc menu ring l.</p>
    338 
    339 
    340 <h3 id="ChangingTheMenu">Thay i cc mc menu vo thi gian chy</h3>
    341 
    342 <p>Sau khi h thng gi {@link android.app.Activity#onCreateOptionsMenu(Menu)
    343 onCreateOptionsMenu()}, n s gi li mt thc th ca {@link android.view.Menu} m bn a vo v
    344 s khng gi li {@link android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()}
    345 tr khi menu b v hiu ha v l do no . Tuy nhin, bn ch nn s dng {@link
    346 android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()}  to trng thi menu
    347 ban u ch khng phi  thc hin thay i trong vng i ca hot ng.</p>
    348 
    349 <p>Nu bn mun sa i menu ty chn da trn 
    350 cc s kin xy ra trong vng i ca hot ng, bn c th lm vy trong phng php
    351  {@link android.app.Activity#onPrepareOptionsMenu(Menu) onPrepareOptionsMenu()}. Phng php
    352 ny chuyn cho bn i tng {@link android.view.Menu} nh hin ang c  bn c th sa i n,
    353 chng hn nh thm, xa b, hoc v hiu ha cc mc. (Phn on cng cung cp lnh gi li {@link
    354 android.app.Fragment#onPrepareOptionsMenu onPrepareOptionsMenu()}.)</p>
    355 
    356 <p>Trn phin bn Android 2.3.x v thp hn, h thng gi {@link
    357 android.app.Activity#onPrepareOptionsMenu(Menu)
    358 onPrepareOptionsMenu()} mi ln ngi dng m menu ty chn (nhn nt <em>Menu</em>
    359 ).</p>
    360 
    361 <p>Trn phin bn Android 3.0 tr ln, menu ty chn c coi nh lun m khi cc mc menu c
    362 trnh by trong thanh hnh ng. Khi mt s kin xy ra v bn mun thc hin mt cp nht menu, bn phi
    363 gi {@link android.app.Activity#invalidateOptionsMenu invalidateOptionsMenu()}  yu cu
    364 h thng gi {@link android.app.Activity#onPrepareOptionsMenu(Menu) onPrepareOptionsMenu()}.</p>
    365 
    366 <p class="note"><strong>Lu :</strong> 
    367 Bn khng nn thay i cc mc trong menu ty chn da trn {@link android.view.View} ang
    368 trong tiu im. Khi  ch  cm ng (khi ngi dng khng s dng bi xoay hay d-pad), cc dng xem
    369 khng th ly tiu im, v th bn khng nn s dng tiu im lm c s  sa i
    370 cc mc trong menu ty chn. Nu bn mun cung cp cc mc menu nhy cm vi ng cnh cho mt {@link
    371 android.view.View}, hy s dng mt <a href="#context-menu">Menu Ng cnh</a>.</p>
    372 
    373 
    374 
    375 
    376 <h2 id="context-menu">To mt Menu Ng cnh</h2>
    377 
    378 <div class="figure" style="width:420px;margin-top:-1em">
    379   <img src="{@docRoot}images/ui/menu-context.png" alt="" />
    380   <p class="img-caption"><strong>Hnh 3.</strong> nh chp mn hnh mt menu ng cnh ni (tri)
    381 v thanh hnh ng ng cnh (phi).</p>
    382 </div>
    383 
    384 <p>Menu ng cnh s a ra cc hnh ng nh hng ti mt mc hoc khung ng cnh c th trong UI. Bn
    385 c th cung cp mt menu ng cnh cho bt k dng xem no, nhng chng thng c s dng nhiu nht cho cc mc trong mt {@link
    386 android.widget.ListView}, {@link android.widget.GridView}, hoc cc b su tp dng xem khc m
    387 ngi dng c th thc hin hnh ng trc tip trn mi mc.</p>
    388 
    389 <p>C hai cch  cung cp cc hnh ng ng cnh:</p>
    390 <ul>
    391   <li>Trong mt <a href="#FloatingContextMenu">menu ng cnh ni</a>. Menu xut hin nh mt
    392 danh sch ni gm nhiu mc menu (tng t nh mt hp thoi) khi ngi dng thc hin nhp gi (nhn v
    393 gi) trn mt dng xem c khai bo h tr menu ng cnh. Ngi dng c th thc hin hnh ng
    394 ng cnh trn mt mc vo mt thi im.</li>
    395 
    396   <li>Trong <a href="#CAB">ch  hnh ng theo ng cnh</a>. Ch  ny l mt h thng trin khai
    397 {@link android.view.ActionMode} c chc nng hin th mt <em>thanh hnh ng ng cnh</em>  bn trn
    398 mn hnh vi cc mc hnh ng nh hng ti (cc) mc c chn. Khi ch  ny hin hot, ngi dng
    399 c th thc hin mt hnh ng trn nhiu mc ngay lp tc (nu ng dng ca bn cho php).</li>
    400 </ul>
    401 
    402 <p class="note"><strong>Lu :</strong> Ch  hnh ng theo ng cnh sn c trn phin bn Android 3.0 (API
    403 mc 11) v cao hn v l k thut c u tin cho vic hin th cc hnh ng theo ng cnh khi
    404 sn c. Nu ng dng ca bn h tr cc phin bn thp hn 3.0, vy bn nn quay li menu ng cnh
    405 ni trn nhng thit b .</p>
    406 
    407 
    408 <h3 id="FloatingContextMenu">To mt menu ng cnh ni</h3>
    409 
    410 <p> cung cp mt menu ng cnh ni:</p>
    411 <ol>
    412   <li>ng k {@link android.view.View} m menu ng cnh nn c lin kt vi bng cch
    413 gi {@link android.app.Activity#registerForContextMenu(View) registerForContextMenu()} v chuyn
    414 cho n {@link android.view.View}.
    415   <p>Nu hot ng ca bn s dng mt {@link android.widget.ListView} hoc {@link android.widget.GridView} v
    416 bn mun tng mc cung cp cng menu ng cnh, hy ng k tt c mc cho mt menu ng cnh bng cch
    417 chuyn {@link android.widget.ListView} hoc {@link android.widget.GridView} cho {@link
    418 android.app.Activity#registerForContextMenu(View) registerForContextMenu()}.</p>
    419 </li>
    420 
    421   <li>Trin khai phng php {@link
    422 android.view.View.OnCreateContextMenuListener#onCreateContextMenu onCreateContextMenu()}
    423 trong {@link android.app.Activity} hoc {@link android.app.Fragment} ca bn.
    424   <p>Khi dng xem c ng k nhn c mt s kin nhp gi, h thng s gi phng php {@link
    425 android.view.View.OnCreateContextMenuListener#onCreateContextMenu onCreateContextMenu()}
    426 ca bn. y l ni bn nh ngha cc mc menu, thng bng cch bung mt ti nguyn menu. V
    427 d:</p>
    428 <pre>
    429 &#64;Override
    430 public void onCreateContextMenu(ContextMenu menu, View v,
    431                                 ContextMenuInfo menuInfo) {
    432     super.onCreateContextMenu(menu, v, menuInfo);
    433     MenuInflater inflater = getMenuInflater();
    434     inflater.inflate(R.menu.context_menu, menu);
    435 }
    436 </pre>
    437 
    438 <p>{@link android.view.MenuInflater} cho php bn bung menu ng cnh t mt <a href="{@docRoot}guide/topics/resources/menu-resource.html">ti nguyn menu</a>. Cc tham s ca phng php gi li
    439 bao gm {@link android.view.View}
    440 m ngi dng  chn v mt i tng {@link android.view.ContextMenu.ContextMenuInfo} cung cp
    441 thng tin b sung v mc c chn. Nu hot ng ca bn c mt vi dng xem m mi dng cung cp
    442 mt menu ng cnh khc nhau, bn c th s dng nhng tham s ny  xc nh menu ng cnh no cn
    443 bung.</p>
    444 </li>
    445 
    446 <li>Trin khai {@link android.app.Activity#onContextItemSelected(MenuItem)
    447 onContextItemSelected()}.
    448   <p>Khi ngi dng chn mt mc menu, h thng s gi phng php ny  bn c th thc hin
    449 hnh ng ph hp. V d:</p>
    450 
    451 <pre>
    452 &#64;Override
    453 public boolean onContextItemSelected(MenuItem item) {
    454     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    455     switch (item.getItemId()) {
    456         case R.id.edit:
    457             editNote(info.id);
    458             return true;
    459         case R.id.delete:
    460             deleteNote(info.id);
    461             return true;
    462         default:
    463             return super.onContextItemSelected(item);
    464     }
    465 }
    466 </pre>
    467 
    468 <p>Phng php {@link android.view.MenuItem#getItemId()} s truy vn ID cho
    469 mc menu c chn, bn nn gn mc ny cho tng mc menu trong XML bng cch s dng thuc tnh {@code
    470 android:id} nh trnh by trong phn v <a href="#xml">nh ngha mt Menu trong
    471 XML</a>.</p>
    472 
    473 <p>Khi bn x l thnh cng mt mc menu, tr v {@code true}. Nu bn khng x l mc menu,
    474 bn nn chuyn mc menu  ti trin khai siu lp. Nu hot ng ca bn bao gm nhiu phn on,
    475 hot ng s nhn c lnh gi li ny trc. Bng cch gi siu lp khi cha c x l, h thng
    476 s chuyn s kin ti phng php gi li tng ng trong tng phn on, ln lt (theo th t
    477 thm phn on) ti khi {@code true} hoc {@code false} c tr v. (Trin khai
    478 mc nh cho {@link android.app.Activity} v {@code android.app.Fragment} s tr v {@code
    479 false}, v th bn nn lun gi siu lp khi cha c x l.)</p>
    480 </li>
    481 </ol>
    482 
    483 
    484 <h3 id="CAB">S dng ch  hnh ng theo ng cnh</h3>
    485 
    486 <p>Ch  hnh ng theo ng cnh l mt trin khai h thng {@link android.view.ActionMode}
    487 tp trung vo tng tc ngi dng hng ti vic thc hin cc hnh ng theo ng cnh. Khi mt
    488 ngi dng kch hot ch  ny bng cch chn mt mc, mt <em>thanh hnh ng ng cnh</em> s xut hin bn trn
    489 mn hnh  trnh by cc hnh ng m ngi dng c th thc hin trn (cc) mc ang c chn. Trong khi
    490 ch  ny c kch hot, ngi dng c th chn nhiu mc (nu bn cho php), b chn mc, v tip tc
    491 iu hng trong hot ng (min l bn sn lng cho php). Ch  hnh ng b v hiu ha
    492 v thanh hnh ng ng cnh bin mt khi ngi dng b chn tt c cc mc, nhn nt QUAY LI,
    493 hoc chn hnh ng <em>Xong</em>  pha bn tri ca thanh.</p>
    494 
    495 <p class="note"><strong>Lu :</strong> Thanh hnh ng ng cnh khng nht thit
    496 phi c lin kt vi <a href="{@docRoot}guide/topics/ui/actionbar.html">thanh hnh ng</a>. Chng vn hnh
    497 c lp, mc d thanh hnh ng ng cnh  ln v tr ca thanh hnh ng
    498 v mt hin th.</p>
    499 
    500 <p>Nu bn ang pht trin cho phin bn Android 3.0 (API mc 11) hoc cao hn, bn
    501 nn s dng ch  hnh ng theo ng cnh  trnh by cc hnh ng ng cnh, thay v s dng <a href="#FloatingContextMenu">menu ng cnh ni</a>.</p>
    502 
    503 <p>i vi cc dng xem cung cp hnh ng ng cnh, bn nn thng xuyn gi ra ch  hnh ng theo ng cnh
    504 khi xy ra mt trong hai s kin sau (hoc c hai):</p>
    505 <ul>
    506   <li>Ngi dng thc hin nhp gi trn dng xem.</li>
    507   <li>Ngi dng chn mt hp kim hoc mt thnh phn UI tng t trong dng xem.</li>
    508 </ul>
    509 
    510 <p>Cch ng dng ca bn gi ra ch  hnh ng theo ng cnh v nh ngha hnh vi cho tng
    511 hnh ng ph thuc vo thit k ca bn. C bn c hai thit k:</p>
    512 <ul>
    513   <li>i vi cc hnh ng ng cnh trn cc dng xem ring l, ty .</li>
    514   <li>i vi cc hnh ng ng cnh hng lot trn cc nhm mc trong mt {@link
    515 android.widget.ListView} hoc {@link android.widget.GridView} (cho php ngi dng chn nhiu
    516 mc v thc hin mt hnh ng trn tt c).</li>
    517 </ul>
    518 
    519 <p>Cc phn sau m t phn thit lp cn thit i vi tng kch bn.</p>
    520 
    521 
    522 <h4 id="CABforViews">Kch hot ch  hnh ng theo ng cnh cho cc dng xem ring l</h4>
    523 
    524 <p>Nu mun gi ra ch  hnh ng theo ng cnh ch khi ngi dng chn cc dng xem
    525 c th, bn nn:</p>
    526 <ol>
    527   <li>Trin khai giao din {@link android.view.ActionMode.Callback}. Trong cc phng php gi li ca giao din, bn
    528 c th quy nh cc hnh ng cho thanh hnh ng ng cnh, hi p cc s kin nhp trn mc hnh ng, v
    529 x l cc s kin vng i khc i vi ch  hnh ng.</li>
    530   <li>Gi {@link android.app.Activity#startActionMode startActionMode()} khi bn mun hin th
    531 thanh (chng hn nh khi ngi dng nhp gi dng xem).</li>
    532 </ol>
    533 
    534 <p>V d:</p>
    535 
    536 <ol>
    537   <li>Trin khai giao din {@link android.view.ActionMode.Callback ActionMode.Callback}:
    538 <pre>
    539 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
    540 
    541     // Called when the action mode is created; startActionMode() was called
    542     &#64;Override
    543     public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    544         // Inflate a menu resource providing context menu items
    545         MenuInflater inflater = mode.getMenuInflater();
    546         inflater.inflate(R.menu.context_menu, menu);
    547         return true;
    548     }
    549 
    550     // Called each time the action mode is shown. Always called after onCreateActionMode, but
    551     // may be called multiple times if the mode is invalidated.
    552     &#64;Override
    553     public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    554         return false; // Return false if nothing is done
    555     }
    556 
    557     // Called when the user selects a contextual menu item
    558     &#64;Override
    559     public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    560         switch (item.getItemId()) {
    561             case R.id.menu_share:
    562                 shareCurrentItem();
    563                 mode.finish(); // Action picked, so close the CAB
    564                 return true;
    565             default:
    566                 return false;
    567         }
    568     }
    569 
    570     // Called when the user exits the action mode
    571     &#64;Override
    572     public void onDestroyActionMode(ActionMode mode) {
    573         mActionMode = null;
    574     }
    575 };
    576 </pre>
    577 
    578 <p>Lu  rng nhng phng php gi li s kin ny hu nh ging vi cc phng php gi li i vi <a href="#options-menu">menu ty chn</a>, khc  ch tng phng php cng chuyn i tng {@link
    579 android.view.ActionMode} c lin kt vi s kin . Bn c th s dng cc API {@link
    580 android.view.ActionMode}  thc hin nhng thay i khc nhau vi CAB, chng hn nh sa i tiu  v
    581 ph  bng {@link android.view.ActionMode#setTitle setTitle()} v {@link
    582 android.view.ActionMode#setSubtitle setSubtitle()} (hu ch khi mun cho bit c bao nhiu mc
    583 c chn).</p>
    584 
    585 <p>Cng lu  rng cc b mu trn s t bin {@code mActionMode} l rng khi
    586 ch  hnh ng b hy.  bc tip theo, bn s thy cch n c khi to v vic lu
    587 bin thnh vin trong hot ng hoc phn on ca bn c th hu ch nh th no.</p>
    588 </li>
    589 
    590   <li>Gi {@link android.app.Activity#startActionMode startActionMode()}  kch hot ch  hnh ng theo ng cnh
    591 khi ph hp, chng hn nh  hi p li mt s kin nhp gi trn mt {@link
    592 android.view.View}:</p>
    593 
    594 <pre>
    595 someView.setOnLongClickListener(new View.OnLongClickListener() {
    596     // Called when the user long-clicks on someView
    597     public boolean onLongClick(View view) {
    598         if (mActionMode != null) {
    599             return false;
    600         }
    601 
    602         // Start the CAB using the ActionMode.Callback defined above
    603         mActionMode = getActivity().startActionMode(mActionModeCallback);
    604         view.setSelected(true);
    605         return true;
    606     }
    607 });
    608 </pre>
    609 
    610 <p>Khi bn gi {@link android.app.Activity#startActionMode startActionMode()}, h thng s tr v
    611 {@link android.view.ActionMode} c to. Bng cch lu iu ny trong mt bin thnh vin, bn c th
    612 thc hin thay i thanh hnh ng theo ng cnh  hi p nhng s kin khc. Trong mu trn, 
    613 {@link android.view.ActionMode} c s dng  m bo rng thc th {@link android.view.ActionMode} khng
    614 c to li nu n  hin hot, bng cch kim tra xem thnh vin c rng khng trc khi khi ng
    615 ch  hnh ng.</p>
    616 </li>
    617 </ol>
    618 
    619 
    620 
    621 <h4 id="CABforListView">Kch hot hnh ng theo ng cnh hng lot trong ListView hoc GridView</h4>
    622 
    623 <p>Nu bn c mt b su tp cc mc trong mt {@link android.widget.ListView} hoc {@link
    624 android.widget.GridView} (hoc mt phn m rng khc ca {@link android.widget.AbsListView}) v mun
    625 cho php ngi dng thc hin cc hnh ng hng lot, bn nn:</p>
    626 
    627 <ul>
    628   <li>Trin khai giao din {@link android.widget.AbsListView.MultiChoiceModeListener} v t n
    629 cho nhm dng xem bng {@link android.widget.AbsListView#setMultiChoiceModeListener
    630 setMultiChoiceModeListener()}. Trong cc phng php gi li ca trnh nghe, bn c th quy nh cc hnh ng
    631 cho thanh hnh ng theo ng cnh, hi p cc s kin nhp trn cc mc hnh ng, v x l cc phng php gi li khc
    632 c k tha t giao din {@link android.view.ActionMode.Callback}.</li>
    633 
    634   <li>Gi {@link android.widget.AbsListView#setChoiceMode setChoiceMode()} bng tham i {@link
    635 android.widget.AbsListView#CHOICE_MODE_MULTIPLE_MODAL}.</li>
    636 </ul>
    637 
    638 <p>V d:</p>
    639 
    640 <pre>
    641 ListView listView = getListView();
    642 listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    643 listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
    644 
    645     &#64;Override
    646     public void onItemCheckedStateChanged(ActionMode mode, int position,
    647                                           long id, boolean checked) {
    648         // Here you can do something when items are selected/de-selected,
    649         // such as update the title in the CAB
    650     }
    651 
    652     &#64;Override
    653     public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    654         // Respond to clicks on the actions in the CAB
    655         switch (item.getItemId()) {
    656             case R.id.menu_delete:
    657                 deleteSelectedItems();
    658                 mode.finish(); // Action picked, so close the CAB
    659                 return true;
    660             default:
    661                 return false;
    662         }
    663     }
    664 
    665     &#64;Override
    666     public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    667         // Inflate the menu for the CAB
    668         MenuInflater inflater = mode.getMenuInflater();
    669         inflater.inflate(R.menu.context, menu);
    670         return true;
    671     }
    672 
    673     &#64;Override
    674     public void onDestroyActionMode(ActionMode mode) {
    675         // Here you can make any necessary updates to the activity when
    676         // the CAB is removed. By default, selected items are deselected/unchecked.
    677     }
    678 
    679     &#64;Override
    680     public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    681         // Here you can perform updates to the CAB due to
    682         // an {@link android.view.ActionMode#invalidate} request
    683         return false;
    684     }
    685 });
    686 </pre>
    687 
    688 <p>Vy l xong. Lc ny, khi ngi dng chn mt mc bng nhp gi, h thng s gi phng php {@link
    689 android.widget.AbsListView.MultiChoiceModeListener#onCreateActionMode onCreateActionMode()}
    690 v hin th thanh hnh ng theo ng cnh vi cc hnh ng c quy nh. Trong khi thanh hnh ng theo ng cnh
    691 hin th, ngi dng c th chn thm mc.</p>
    692 
    693 <p>Trong mt s trng hp m cc hnh ng ng cnh cung cp cc mc hnh ng chung, bn c th mun
    694 thm mt hp kim hoc mt phn t UI tng t  cho php ngi dng chn cc mc, v
    695 h c th khng pht hin c hnh vi nhp gi. Khi mt ngi dng chn hp kim, bn
    696 c th gi ra ch  hnh ng theo ng cnh bng cch thit t mc danh sch tng ng v trng thi
    697  chn bng {@link android.widget.AbsListView#setItemChecked setItemChecked()}.</p>
    698 
    699 
    700 
    701 
    702 <h2 id="PopupMenu">To mt Menu Bt ln</h2>
    703 
    704 <div class="figure" style="width:220px">
    705 <img src="{@docRoot}images/ui/popupmenu.png" alt="" />
    706 <p><strong>Hnh 4.</strong> Menu bt ln trong ng dng Gmail, c neo vo nt trn
    707  trn cng bn phi.</p>
    708 </div>
    709 
    710 <p>{@link android.widget.PopupMenu} l mt menu m thi c neo vo mt {@link android.view.View}.
    711 N xut hin bn di dng xem du neo nu c khong trng, hoc bn trn dng xem nu khng. N c ch cho vic:</p>
    712 <ul>
    713   <li>Cung cp mt menu kiu trn cho cc hnh ng m <em>lin quan ti</em> ni dung c th (chng hn nh tiu  e-mail
    714 ca Gmail nh minh ha trong hnh 4).
    715     <p class="note"><strong>Lu :</strong> N khng ging nh mt menu ng cnh, vn thng
    716 p dng cho cc hnh ng m <em>nh hng</em> ti ni dung c chn. i vi nhng hnh ng nh hng ti ni dung
    717 c chn, hy s dng <a href="#CAB">ch  hnh ng theo ng cnh</a> hoc <a href="#FloatingContextMenu">menu ng cnh ni</a>.</p></li>
    718   <li>Cung cp mt phn th hai ca cu lnh (chng hn nh mt nt c nh du "Thm"
    719 c chc nng to ra mt menu bt ln vi cc ty chn "Thm" khc nhau).</li>
    720   <li>Cung cp mt danh sch th xung tng t nh {@link android.widget.Spinner}, n khng gi li mt
    721 la chn lin tc.</li>
    722 </ul>
    723 
    724 
    725 <p class="note"><strong>Lu :</strong> {@link android.widget.PopupMenu} sn c vi API
    726 mc 11 tr ln.</p>
    727 
    728 <p>Nu bn nh ngha <a href="#xml">menu ca mnh trong XML</a>, sau y l cch bn c th hin th menu bt ln:</p>
    729 <ol>
    730   <li>Khi to mt {@link android.widget.PopupMenu} bng hm dng ca n, c chc nng a
    731 ng dng hin ti {@link android.content.Context} v {@link android.view.View} ti menu
    732 m s c neo.</li>
    733   <li>S dng {@link android.view.MenuInflater}  bung ti nguyn menu ca bn vo i tng {@link
    734 android.view.Menu} c tr v bi {@link
    735 android.widget.PopupMenu#getMenu() PopupMenu.getMenu()}. Trn API mc 14 tr ln, bn c th s dng
    736 {@link android.widget.PopupMenu#inflate PopupMenu.inflate()} thay th.</li>
    737   <li>Gi {@link android.widget.PopupMenu#show() PopupMenu.show()}.</li>
    738 </ol>
    739 
    740 <p>V d, sau y l mt nt vi thuc tnh {@link android.R.attr#onClick android:onClick} c chc nng
    741 hin th mt menu bt ln:</p>
    742 
    743 <pre>
    744 &lt;ImageButton
    745     android:layout_width="wrap_content" 
    746     android:layout_height="wrap_content" 
    747     android:src="@drawable/ic_overflow_holo_dark"
    748     android:contentDescription="@string/descr_overflow_button"
    749     android:onClick="showPopup" />
    750 </pre>
    751 
    752 <p>Khi , hot ng c th hin th menu bt ln nh sau:</p>
    753 
    754 <pre>
    755 public void showPopup(View v) {
    756     PopupMenu popup = new PopupMenu(this, v);
    757     MenuInflater inflater = popup.getMenuInflater();
    758     inflater.inflate(R.menu.actions, popup.getMenu());
    759     popup.show();
    760 }
    761 </pre>
    762 
    763 <p>Trong API mc 14 tr ln, bn c th kt hp hai dng c chc nng bung menu bng {@link
    764 android.widget.PopupMenu#inflate PopupMenu.inflate()}.</p>
    765 
    766 <p>Menu b b qua khi ngi dng chn mt mc hoc chm vo bn ngoi vng
    767 menu. Bn c th lng nghe bo hiu s kin b bng cch s dng {@link
    768 android.widget.PopupMenu.OnDismissListener}.</p>
    769 
    770 <h3 id="PopupEvents">X l s kin nhp</h3>
    771 
    772 <p> thc hin mt
    773 hnh ng khi ngi dng chn mt mc menu, bn phi trin khai giao din {@link
    774 android.widget.PopupMenu.OnMenuItemClickListener} v ng k n vi {@link
    775 android.widget.PopupMenu} ca mnh bng cch gi {@link android.widget.PopupMenu#setOnMenuItemClickListener
    776 setOnMenuItemclickListener()}. Khi ngi dng chn mt mc, h thng s gi lnh gi li {@link
    777 android.widget.PopupMenu.OnMenuItemClickListener#onMenuItemClick onMenuItemClick()} trong
    778 giao din ca bn.</p>
    779 
    780 <p>V d:</p>
    781 
    782 <pre>
    783 public void showMenu(View v) {
    784     PopupMenu popup = new PopupMenu(this, v);
    785 
    786     // This activity implements OnMenuItemClickListener
    787     popup.setOnMenuItemClickListener(this);
    788     popup.inflate(R.menu.actions);
    789     popup.show();
    790 }
    791 
    792 &#64;Override
    793 public boolean onMenuItemClick(MenuItem item) {
    794     switch (item.getItemId()) {
    795         case R.id.archive:
    796             archive(item);
    797             return true;
    798         case R.id.delete:
    799             delete(item);
    800             return true;
    801         default:
    802             return false;
    803     }
    804 }
    805 </pre>
    806 
    807 
    808 <h2 id="groups">To Nhm Menu</h2>
    809 
    810 <p>Nhm menu l mt tp hp cc mc menu chia s nhng c im nht nh. Vi mt nhm, bn c th
    811 :</p>
    812 <ul>
    813   <li>Hin th hoc n tt c cc mc bng {@link android.view.Menu#setGroupVisible(int,boolean)
    814 setGroupVisible()}</li>
    815   <li>Kch hot hoc v hiu ha tt c cc mc bng {@link android.view.Menu#setGroupEnabled(int,boolean)
    816 setGroupEnabled()}</li>
    817   <li>Quy nh xem tt c cc mc c th chn hay khng bng {@link
    818 android.view.Menu#setGroupCheckable(int,boolean,boolean) setGroupCheckable()}</li>
    819 </ul>
    820 
    821 <p>Bn c th to mt nhm bng cch lng cc phn t {@code &lt;item&gt;} bn trong mt phn t {@code &lt;group&gt;}
    822 vo ti nguyn menu ca bn hoc bng cch quy nh mt ID nhm bng phng php {@link
    823 android.view.Menu#add(int,int,int,int) add()}.</p>
    824 
    825 <p>Sau y l mt v d v ti nguyn menu bao gm mt nhm:</p>
    826 
    827 <pre>
    828 &lt;?xml version="1.0" encoding="utf-8"?&gt;
    829 &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    830     &lt;item android:id="@+id/menu_save"
    831           android:icon="@drawable/menu_save"
    832           android:title="@string/menu_save" /&gt;
    833     &lt;!-- menu group --&gt;
    834     &lt;group android:id="@+id/group_delete"&gt;
    835         &lt;item android:id="@+id/menu_archive"
    836               android:title="@string/menu_archive" /&gt;
    837         &lt;item android:id="@+id/menu_delete"
    838               android:title="@string/menu_delete" /&gt;
    839     &lt;/group&gt;
    840 &lt;/menu&gt;
    841 </pre>
    842 
    843 <p>Cc mc nm trong nhm xut hin  cng cp nh mc u tin&mdash;tt c ba mc
    844 trong menu u l cc mc ng cp. Tuy nhin, bn c th sa i cc c im ca hai
    845 mc trong nhm bng cch tham chiu ID nhm v s dng cc phng php c lit k bn trn. H thng cng s
    846 khng bao gi tch ring cc mc  ghp nhm. V d, nu bn khai bo {@code
    847 android:showAsAction="ifRoom"} cho tng mc, chng s hoc u xut hin trong thanh hnh ng
    848 hoc u xut hin trong phn trn hnh ng.</p>
    849 
    850 
    851 <h3 id="checkable">S dng mc menu c th chn</h3>
    852 
    853 <div class="figure" style="width:200px">
    854   <img src="{@docRoot}images/radio_buttons.png" height="333" alt="" />
    855   <p class="img-caption"><strong>Hnh 5.</strong> nh chp mn hnh mt menu con vi cc mc
    856 c th chn.</p>
    857 </div>
    858 
    859 <p>Mt mc c th c ch nh mt giao din  bt v tt cc ty chn, bng cch s dng mt hp kim cho
    860 cc ty chn c lp, hoc nt chn mt cho cc nhm
    861 ty chn loi tr ln nhau. Hnh 5 minh ha mt menu con vi cc mc c th chn bng cc nt
    862  chn mt.</p>
    863 
    864 <p class="note"><strong>Lu :</strong> Cc mc menu trong Menu Biu tng (t menu ty chn) khng th
    865 hin th mt hp kim hay nt chn mt. Nu bn chn t cc mc trong Menu Biu tng l c th chn,
    866 bn phi ch nh trng thi c chn bng cch tro i biu tng v/hoc vn bn
    867 mi ln trng thi thay i mt cch th cng.</p>
    868 
    869 <p>Bn c th nh ngha hnh vi c th chn cho cc mc menu ring l bng cch s dng thuc tnh {@code
    870 android:checkable} trong phn t {@code &lt;item&gt;}, hoc cho ton b nhm vi
    871 thuc tnh {@code android:checkableBehavior} trong phn t {@code &lt;group&gt;}. V
    872 d, tt c cc mc trong nhm menu ny c th chn bng mt nt chn mt:</p>
    873 
    874 <pre>
    875 &lt;?xml version="1.0" encoding="utf-8"?&gt;
    876 &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    877     &lt;group android:checkableBehavior="single"&gt;
    878         &lt;item android:id="@+id/red"
    879               android:title="@string/red" /&gt;
    880         &lt;item android:id="@+id/blue"
    881               android:title="@string/blue" /&gt;
    882     &lt;/group&gt;
    883 &lt;/menu&gt;
    884 </pre>
    885 
    886 <p>Thuc tnh {@code android:checkableBehavior} chp nhn hoc:
    887 <dl>
    888   <dt>{@code single}</dt>
    889     <dd>Ch chn c mt mc t nhm (nt chn mt)</dd>
    890   <dt>{@code all}</dt>
    891     <dd>C th chn c tt c cc mc (hp kim)</dd>
    892   <dt>{@code none}</dt>
    893     <dd>Khng chn c mc no</dd>
    894 </dl>
    895 
    896 <p>Bn c th p dng mt trng thi c chn mc nh cho mt mc bng cch s dng thuc tnh {@code android:checked} trong
    897 phn t {@code &lt;item&gt;} v thay i n trong m bng phng php {@link
    898 android.view.MenuItem#setChecked(boolean) setChecked()}.</p>
    899 
    900 <p>Khi chn mt mc c th chn, h thng s gi phng php gi li mc c chn tng ng
    901 (chng hn nh {@link android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()}). Chnh
    902  y bn phi t trng thi ca hp kim, v hp kim hay nt chn mt u khng
    903 t ng thay i trng thi ca n. Bn c th truy vn trng thi hin ti ca mc (nh trc khi
    904 ngi dng chn) bng {@link android.view.MenuItem#isChecked()} v sau  t trng thi c chn bng
    905 {@link android.view.MenuItem#setChecked(boolean) setChecked()}. V d:</p>
    906 
    907 <pre>
    908 &#64;Override
    909 public boolean onOptionsItemSelected(MenuItem item) {
    910     switch (item.getItemId()) {
    911         case R.id.vibrate:
    912         case R.id.dont_vibrate:
    913             if (item.isChecked()) item.setChecked(false);
    914             else item.setChecked(true);
    915             return true;
    916         default:
    917             return super.onOptionsItemSelected(item);
    918     }
    919 }
    920 </pre>
    921 
    922 <p>Nu bn khng t trng thi c chn bng cch ny, khi  trng thi hin th ca mc (hp kim hoc
    923 nt chn mt) s khng
    924 thay i khi ngi dng chn n. Khi bn t trng thi, hot ng s gi nguyn trng thi c chn
    925 ca mc   khi ngi dng m menu sau, trng thi c chn m bn t
    926 s c hin th.</p>
    927 
    928 <p class="note"><strong>Lu :</strong>
    929 Cc mc menu c th chn c ch nh s dng trn mi phin v khng c lu sau khi
    930 ng dng b hy. Nu bn c cc ci t ng dng m bn mun lu cho ngi dng,
    931 bn nn lu tr d liu bng cch s dng <a href="{@docRoot}guide/topics/data/data-storage.html#pref">Ty chn dng chung</a>.</p>
    932 
    933 
    934 
    935 <h2 id="intents">Thm Mc Menu da trn  nh</h2>
    936 
    937 <p>i khi bn s mun mt mc menu khi chy mt hot ng bng cch s dng mt {@link android.content.Intent}
    938 (d  l mt hot ng trong ng dng ca bn hay mt ng dng khc). Khi bn bit  nh m mnh
    939 mun s dng v c mt mc menu c th s khi to  nh, bn c th thc thi  nh
    940 bng {@link android.app.Activity#startActivity(Intent) startActivity()} trong phng php gi li
    941 ph hp theo mc c chn (chng hn nh lnh gi li {@link
    942 android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()}).</p>
    943 
    944 <p>Tuy nhin, nu bn khng chc chn rng thit b ca ngi dng
    945 cha mt ng dng x l  nh  th vic thm mt mc menu gi n ra c th dn n
    946 mc menu khng hot ng, do  nh c th khng phn gii thnh mt
    947 hot ng.  gii quyt iu ny, Android cho php bn linh hot thm cc mc menu vo menu ca mnh
    948 khi Android tm cc hot ng trn thit b  x l  nh ca bn.</p>
    949 
    950 <p> thm cc mc menu da trn cc hot ng sn c m chp nhn  nh:</p>
    951 <ol>
    952   <li>nh ngha mt
    953  nh bng th loi {@link android.content.Intent#CATEGORY_ALTERNATIVE} v/hoc
    954 {@link android.content.Intent#CATEGORY_SELECTED_ALTERNATIVE}, cng vi bt k yu cu no khc.</li>
    955   <li>Gi {@link
    956 android.view.Menu#addIntentOptions(int,int,int,ComponentName,Intent[],Intent,int,MenuItem[])
    957 Menu.addIntentOptions()}. Sau , Android tm kim bt k ng dng no c th thc hin  nh
    958 v thm chng vo menu ca bn.</li>
    959 </ol>
    960 
    961 <p>Nu khng c ng dng c ci t
    962 m tha mn  nh th khng c mc menu no c thm vo.</p>
    963 
    964 <p class="note"><strong>Lu :</strong>
    965 {@link android.content.Intent#CATEGORY_SELECTED_ALTERNATIVE} c s dng  x l
    966 phn t ang c chn trn mn hnh. V vy, n ch nn c s dng khi to mt Menu trong {@link
    967 android.app.Activity#onCreateContextMenu(ContextMenu,View,ContextMenuInfo)
    968 onCreateContextMenu()}.</p>
    969 
    970 <p>V d:</p>
    971 
    972 <pre>
    973 &#64;Override
    974 public boolean onCreateOptionsMenu(Menu menu){
    975     super.onCreateOptionsMenu(menu);
    976 
    977     // Create an Intent that describes the requirements to fulfill, to be included
    978     // in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE.
    979     Intent intent = new Intent(null, dataUri);
    980     intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    981 
    982     // Search and populate the menu with acceptable offering applications.
    983     menu.addIntentOptions(
    984          R.id.intent_group,  // Menu group to which new items will be added
    985          0,      // Unique item ID (none)
    986          0,      // Order for the items (none)
    987          this.getComponentName(),   // The current activity name
    988          null,   // Specific items to place first (none)
    989          intent, // Intent created above that describes our requirements
    990          0,      // Additional flags to control items (none)
    991          null);  // Array of MenuItems that correlate to specific items (none)
    992 
    993     return true;
    994 }</pre>
    995 
    996 <p>i vi mi hot ng c tm thy m cung cp mt b lc  nh khp vi  nh c nh ngha, mt mc menu
    997 c thm, bng cch s dng gi tr trong <code>android:label</code> ca b lc  nh lm
    998 tiu  ca mc menu v biu tng ca ng dng lm biu tng ca mc menu. Phng php
    999 {@link android.view.Menu#addIntentOptions(int,int,int,ComponentName,Intent[],Intent,int,MenuItem[])
   1000 addIntentOptions()} tr v s mc menu c thm.</p>
   1001 
   1002 <p class="note"><strong>Lu :</strong> Khi bn gi {@link
   1003 android.view.Menu#addIntentOptions(int,int,int,ComponentName,Intent[],Intent,int,MenuItem[])
   1004 addIntentOptions()}, n s khng ch bt k v tt c cc mc menu theo nhm menu c quy nh trong tham i
   1005 u tin.</p>
   1006 
   1007 
   1008 <h3 id="AllowingToAdd">Cho php hot ng ca bn c thm vo cc menu khc</h3>
   1009 
   1010 <p>Bn cng c th cung cp cc dch v ca hot ng ca mnh cho cc ng dng khc, v vy ng dng ca bn
   1011 c th nm trong menu ca cc ng dng khc (o ngc vai tr nu trn).</p>
   1012 
   1013 <p> c nm trong menu ca ng dng khc, bn cn nh ngha mt b lc
   1014  nh nh bnh thng, nhng m bo thm cc gi tr {@link android.content.Intent#CATEGORY_ALTERNATIVE}
   1015 v/hoc {@link android.content.Intent#CATEGORY_SELECTED_ALTERNATIVE} cho th loi
   1016 b lc  nh. V d:</p>
   1017 <pre>
   1018 &lt;intent-filter label="&#64;string/resize_image">
   1019     ...
   1020     &lt;category android:name="android.intent.category.ALTERNATIVE" />
   1021     &lt;category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
   1022     ...
   1023 &lt;/intent-filter>
   1024 </pre>
   1025 
   1026 <p>Tm hiu thm v vic ghi cc b lc  nh trong ti liu
   1027 <a href="/guide/components/intents-filters.html"> nh v B lc  nh</a>.</p>
   1028 
   1029 <p> tham kho mt ng dng mu s dng k thut ny, hy xem m mu 
   1030 <a href="{@docRoot}resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html">Note
   1031 Pad</a>.</p>
   1032