1 page.title= 2 parent.title= 3 parent.link=index.html 4 @jd:body 5 6 <div id="qv-wrapper"> 7 <div id="qv"> 8 <h2></h2> 9 <ol> 10 <li><a href="#xml">XML </a></li> 11 <li><a href="#options-menu"> </a> 12 <ol> 13 <li><a href="#RespondingOptionsMenu"> </a></li> 14 <li><a href="#ChangingTheMenu"> </a></li> 15 </ol> 16 </li> 17 <li><a href="#context-menu"> </a> 18 <ol> 19 <li><a href="#FloatingContextMenu"> </a></li> 20 <li><a href="#CAB"> </a></li> 21 </ol> 22 </li> 23 <li><a href="#PopupMenu"> </a> 24 <ol> 25 <li><a href="#PopupEvents"> </a></li> 26 </ol> 27 </li> 28 <li><a href="#groups"> </a> 29 <ol> 30 <li><a href="#checkable"> </a></li> 31 </ol> 32 </li> 33 <li><a href="#intents"> </a> 34 <ol> 35 <li><a href="#AllowingToAdd"></a></li> 36 </ol> 37 </li> 38 </ol> 39 40 <h2></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></h2> 49 <ol> 50 <li><a href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a></li> 51 <li><a href="{@docRoot}guide/topics/resources/menu-resource.html">Menu Resource</a></li> 52 <li><a href="http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html">Goodbye to the Menu Button</a> 53 </li> 54 </ol> 55 </div> 56 </div> 57 58 <p> {@link android.view.Menu} API 59 60 </p> 61 62 <p>Android 3.0API 11Android <em>Menu</em> 63 Android 6 64 65 </p> 66 67 <p> {@link android.view.Menu} API 68 Android 3 69 70 </p> 71 72 <dl> 73 <dt><strong> </strong></dt> 74 <dd><a href="#options-menu"> </a> 75 76 77 <p>Android 2.3 <em>Menu</em> 78 </p> 79 <p>Android 3.0 <a href="{@docRoot}guide/topics/ui/actionbar.html"></a> 80 Android 3.0 <em>Menu</em> 81 82 83 </p> 84 <p><a href="#options-menu"> </a></p> 85 </dd> 86 87 <dt><strong> </strong></dt> 88 89 <dd> <a href="#FloatingContextMenu"> </a> 90 91 92 <p>Android 3.0 <a href="#CAB"> </a> 93 94 </p> 95 <p><a href="#context-menu"> </a></p> 96 </dd> 97 98 <dt><strong> </strong></dt> 99 <dd> 100 2 101 <strong></strong> 102 103 104 105 <p><a href="#PopupMenu"> </a></p> 106 </dd> 107 </dl> 108 109 110 111 <h2 id="xml">XML </h2> 112 113 <p>Android XML 114 XML <a href="{@docRoot}guide/topics/resources/menu-resource.html"> </a> 115 {@link android.view.Menu} 116 117 </p> 118 119 <p> </p> 120 <ul> 121 <li>XML </li> 122 <li></li> 123 <li> <a href="{@docRoot}guide/topics/resources/index.html"></a> 124 </li> 125 </ul> 126 127 <p> <code>res/menu/</code> XML 128 </p> 129 <dl> 130 <dt><code><menu></code></dt> 131 <dd> {@link android.view.Menu} <code><menu></code> 1 <code><item></code> <code><group></code> 132 133 </dd> 134 135 <dt><code><item></code></dt> 136 <dd> 1 {@link android.view.MenuItem} <code><menu></code> 137 </dd> 138 139 <dt><code><group></code></dt> 140 <dd> {@code <item>} 141 <a href="#groups"> </a> 142 </dd> 143 </dl> 144 145 146 <p><code>game_menu.xml</code> </p> 147 <pre> 148 <?xml version="1.0" encoding="utf-8"?> 149 <menu xmlns:android="http://schemas.android.com/apk/res/android"> 150 <item android:id="@+id/new_game" 151 android:icon="@drawable/ic_new_game" 152 android:title="@string/new_game" 153 android:showAsAction="ifRoom"/> 154 <item android:id="@+id/help" 155 android:icon="@drawable/ic_help" 156 android:title="@string/help" /> 157 </menu> 158 </pre> 159 160 <p><code><item></code> 161 </p> 162 163 <dl> 164 <dt>{@code android:id}</dt> 165 <dd> ID 166 </dd> 167 <dt>{@code android:icon}</dt> 168 <dd></dd> 169 <dt>{@code android:title}</dt> 170 <dd></dd> 171 <dt>{@code android:showAsAction}</dt> 172 <dd><a href="{@docRoot}guide/topics/ui/actionbar.html"></a> </dd> 173 </dl> 174 175 <p><a href="{@docRoot}guide/topics/resources/menu-resource.html">Menu Resource</a> 176 </p> 177 178 <p>{@code <item>} {@code <menu>} 179 PC 180 181 </p> 182 183 <pre> 184 <?xml version="1.0" encoding="utf-8"?> 185 <menu xmlns:android="http://schemas.android.com/apk/res/android"> 186 <item android:id="@+id/file" 187 android:title="@string/file" > 188 <!-- "file" submenu --> 189 <menu> 190 <item android:id="@+id/create_new" 191 android:title="@string/create_new" /> 192 <item android:id="@+id/open" 193 android:title="@string/open" /> 194 </menu> 195 </item> 196 </menu> 197 </pre> 198 199 <p>{@link android.view.MenuInflater#inflate(int,Menu) 200 MenuInflater.inflate()} XML 201 202 </p> 203 204 205 206 <h2 id="options-menu"> </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> 1.</strong> Android 2.3 211 </p> 212 </div> 213 214 <p> 215 </p> 216 217 <p> 218 </p> 219 220 <ul> 221 <li><strong>Android 2.3.xAPI 10</strong> <em>Menu</em> 1 222 223 6 224 225 7 Android 6 <em></em> 226 227 </li> 228 229 <li><strong>Android 3.0API 11</strong> <a href="{@docRoot}guide/topics/ui/actionbar.html"></a> 230 <em>Menu</em> 231 232 {@code <item>} {@code android:showAsAction="ifRoom"} 2 233 234 235 236 <p> <a href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a> </p> 237 <p class="note"><strong>:</strong> Android 3.0 <em></em> 238 Android <a href="{@docRoot}resources/samples/ActionBarCompat/index.html"></a> 239 240 </p> 241 </li> 242 </ul> 243 244 <img src="{@docRoot}images/ui/actionbar.png" alt="" /> 245 <p class="img-caption"><strong> 2.</strong> <a href="{@docRoot}resources/samples/HoneycombGallery/index.html">Honeycomb Gallery</a> 246 </p> 247 248 <p>{@link android.app.Activity} {@link android.app.Fragment} 249 UI 250 251 252 {@code <item>} {@code android:orderInCategory} 253 </p> 254 255 <p> {@link 256 android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} {@link android.app.Fragment#onCreateOptionsMenu onCreateOptionsMenu()} 257 <a href="#xml">XML </a> {@link 258 android.view.Menu} 259 </p> 260 261 <pre> 262 @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>{@link android.view.Menu#add(int,int,int,int) 271 add()} {@link android.view.MenuItem} API {@link android.view.Menu#findItem findItem()} 272 </p> 273 274 <p>Android 2.3.x {@link 275 android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} 276 Android 3.0 {@link android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} 277 278 </p> 279 280 281 282 <h3 id="RespondingOptionsMenu"> </h3> 283 284 <p> {@link android.app.Activity#onOptionsItemSelected(MenuItem) 285 onOptionsItemSelected()} 286 {@link android.view.MenuItem} {@link android.view.MenuItem#getItemId()} ID {@code android:id} {@link android.view.Menu#add(int,int,int,int) add()} 287 288 289 ID 290 </p> 291 292 <pre> 293 @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> {@code true} {@link 310 android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} fause 311 312 </p> 313 314 <p> {@link 315 android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} {@code true} 316 317 </p> 318 319 <p class="note"><strong>:</strong> Android 3.0 {@code android:onClick} XML 320 321 1 {@link android.view.MenuItem} 322 323 <a href="{@docRoot}guide/topics/resources/menu-resource.html">Menu Resource</a></p> 324 325 <p class="note"><strong>:</strong> {@link android.app.Activity#onCreateOptionsMenu(Menu) 326 onCreateOptionsMenu()} {@link android.app.Activity#onOptionsItemSelected(MenuItem) 327 onOptionsItemSelected()} 328 329 330 1 1 {@link android.app.Activity#onCreateOptionsMenu(Menu) 331 onCreateOptionsMenu()} 332 333 334 {@code super.onCreateOptionsMenu(menu)} {@link 335 android.view.Menu#add(int,int,int,int) menu.add()} 336 337 </p> 338 339 340 <h3 id="ChangingTheMenu"> </h3> 341 342 <p> {@link android.app.Activity#onCreateOptionsMenu(Menu) 343 onCreateOptionsMenu()} {@link android.view.Menu} {@link android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} 344 345 {@link 346 android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} 347 </p> 348 349 <p> {@link android.app.Activity#onPrepareOptionsMenu(Menu) onPrepareOptionsMenu()} 350 351 {@link android.view.Menu} 352 353 {@link 354 android.app.Fragment#onPrepareOptionsMenu onPrepareOptionsMenu()} </p> 355 356 <p>Android 2.3.x <em>Menu</em> {@link 357 android.app.Activity#onPrepareOptionsMenu(Menu) 358 onPrepareOptionsMenu()} 359 </p> 360 361 <p>Android 3.0 362 {@link android.app.Activity#invalidateOptionsMenu invalidateOptionsMenu()} {@link android.app.Activity#onPrepareOptionsMenu(Menu) onPrepareOptionsMenu()} 363 364 </p> 365 366 <p class="note"><strong>:</strong> {@link android.view.View} 367 368 369 370 {@link 371 android.view.View} <a href="#context-menu"> </a></p> 372 373 374 375 376 <h2 id="context-menu"> </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> 3.</strong> 381 </p> 382 </div> 383 384 <p> UI {@link 385 android.widget.ListView}{@link android.widget.GridView} 386 387 </p> 388 389 <p> 2 </p> 390 <ul> 391 <li><a href="#FloatingContextMenu"> </a> 392 393 1 394 </li> 395 396 <li><a href="#CAB"> </a> <em> </em> {@link android.view.ActionMode} 397 398 399 </li> 400 </ul> 401 402 <p class="note"><strong>:</strong> Android 3.0API 11 403 404 3.0 405 </p> 406 407 408 <h3 id="FloatingContextMenu"> </h3> 409 410 <p> : </p> 411 <ol> 412 <li>{@link android.app.Activity#registerForContextMenu(View) registerForContextMenu()} {@link android.view.View} {@link android.view.View} 413 414 415 <p> {@link android.widget.ListView} {@link android.widget.GridView} {@link android.widget.ListView} {@link android.widget.GridView} {@link 416 android.app.Activity#registerForContextMenu(View) registerForContextMenu()} 417 418 </p> 419 </li> 420 421 <li>{@link android.app.Activity} {@link android.app.Fragment} {@link 422 android.view.View.OnCreateContextMenuListener#onCreateContextMenu onCreateContextMenu()} 423 424 <p> {@link 425 android.view.View.OnCreateContextMenuListener#onCreateContextMenu onCreateContextMenu()} 426 427 </p> 428 <pre> 429 @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} <a href="{@docRoot}guide/topics/resources/menu-resource.html"> </a> {@link android.view.View} {@link android.view.ContextMenu.ContextMenuInfo} 439 440 441 442 443 </p> 444 </li> 445 446 <li>{@link android.app.Activity#onContextItemSelected(MenuItem) 447 onContextItemSelected()} 448 <p> 449 </p> 450 451 <pre> 452 @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>{@link android.view.MenuItem#getItemId()} ID <a href="#xml">XML </a>{@code 469 android:id} XML 470 471 </p> 472 473 <p> {@code true} 474 475 {@code true} {@code false} 1 476 477 {@link android.app.Activity} {@code android.app.Fragment} {@code 478 false} 479 </p> 480 </li> 481 </ol> 482 483 484 <h3 id="CAB"> </h3> 485 486 <p> {@link android.view.ActionMode} 487 <em> </em> 488 489 490 491 Back <em>Done</em> 492 493 </p> 494 495 <p class="note"><strong>:</strong> <a href="{@docRoot}guide/topics/ui/actionbar.html"></a> 496 497 498 </p> 499 500 <p>Android 3.0API 11<a href="#FloatingContextMenu"> </a> 501 </p> 502 503 <p> 2 504 </p> 505 <ul> 506 <li></li> 507 <li> UI </li> 508 </ul> 509 510 <p> 511 2 </p> 512 <ul> 513 <li> </li> 514 <li>{@link 515 android.widget.ListView} {@link android.widget.GridView} 516 </li> 517 </ul> 518 519 <p></p> 520 521 522 <h4 id="CABforViews"> </h4> 523 524 <p> 525 </p> 526 <ol> 527 <li>{@link android.view.ActionMode.Callback} 528 529 </li> 530 <li>{@link android.app.Activity#startActionMode startActionMode()} 531 </li> 532 </ol> 533 534 <p></p> 535 536 <ol> 537 <li>{@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 @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 @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 @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 @Override 572 public void onDestroyActionMode(ActionMode mode) { 573 mActionMode = null; 574 } 575 }; 576 </pre> 577 578 <p> {@link 579 android.view.ActionMode} <a href="#options-menu"> </a>{@link 580 android.view.ActionMode} API {@link android.view.ActionMode#setTitle setTitle()} {@link 581 android.view.ActionMode#setSubtitle setSubtitle()}CAB 582 583 </p> 584 585 <p> {@code mActionMode} null 586 587 </p> 588 </li> 589 590 <li>{@link android.app.Activity#startActionMode startActionMode()} {@link 591 android.view.View} 592 </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>{@link android.app.Activity#startActionMode startActionMode()} {@link android.view.ActionMode} 611 612 {@link android.view.ActionMode} {@link android.view.ActionMode} null 613 614 615 </p> 616 </li> 617 </ol> 618 619 620 621 <h4 id="CABforListView"> ListView GridView </h4> 622 623 <p>{@link android.widget.ListView} {@link 624 android.widget.GridView} {@link android.widget.AbsListView} 625 </p> 626 627 <ul> 628 <li>{@link android.widget.AbsListView.MultiChoiceModeListener} {@link android.widget.AbsListView#setMultiChoiceModeListener 629 setMultiChoiceModeListener()} ViewGroup 630 {@link android.view.ActionMode.Callback} 631 632 </li> 633 634 <li>{@link 635 android.widget.AbsListView#CHOICE_MODE_MULTIPLE_MODAL} {@link android.widget.AbsListView#setChoiceMode setChoiceMode()} </li> 636 </ul> 637 638 <p></p> 639 640 <pre> 641 ListView listView = getListView(); 642 listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 643 listView.setMultiChoiceModeListener(new MultiChoiceModeListener() { 644 645 @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 @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 @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 @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 @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> {@link 689 android.widget.AbsListView.MultiChoiceModeListener#onCreateActionMode onCreateActionMode()} 690 691 </p> 692 693 <p> UI 694 695 {@link android.widget.AbsListView#setItemChecked setItemChecked()} 696 697 </p> 698 699 700 701 702 <h2 id="PopupMenu"> </h2> 703 704 <div class="figure" style="width:220px"> 705 <img src="{@docRoot}images/ui/popupmenu.png" alt="" /> 706 <p><strong> 4.</strong> Gmail 707 </p> 708 </div> 709 710 <p>{@link android.widget.PopupMenu} {@link android.view.View} 711 </p> 712 <ul> 713 <li><em></em> 4 Gmail 714 715 <p class="note"><strong>:</strong> <em></em> 716 <a href="#CAB"> </a><a href="#FloatingContextMenu"> </a> 717 </p></li> 718 <li> 2 719 </li> 720 <li> {@link android.widget.Spinner} 721 </li> 722 </ul> 723 724 725 <p class="note"><strong>:</strong> {@link android.widget.PopupMenu} API 11 726 </p> 727 728 <p><a href="#xml">XML </a> </p> 729 <ol> 730 <li> {@link android.widget.PopupMenu} {@link android.content.Context} {@link android.view.View} 731 732 </li> 733 <li>{@link android.view.MenuInflater} {@link 734 android.widget.PopupMenu#getMenu() PopupMenu.getMenu()} {@link 735 android.view.Menu} API 14 {@link android.widget.PopupMenu#inflate PopupMenu.inflate()} 736 </li> 737 <li>{@link android.widget.PopupMenu#show() PopupMenu.show()} </li> 738 </ol> 739 740 <p> {@link android.R.attr#onClick android:onClick} 741 </p> 742 743 <pre> 744 <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> </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>API 14 {@link 764 android.widget.PopupMenu#inflate PopupMenu.inflate()} 2 </p> 765 766 <p> 767 {@link 768 android.widget.PopupMenu.OnDismissListener} dismiss </p> 769 770 <h3 id="PopupEvents"> </h3> 771 772 <p> {@link 773 android.widget.PopupMenu.OnMenuItemClickListener} {@link android.widget.PopupMenu#setOnMenuItemClickListener 774 setOnMenuItemclickListener()} {@link 775 android.widget.PopupMenu} 776 {@link 777 android.widget.PopupMenu.OnMenuItemClickListener#onMenuItemClick onMenuItemClick()} 778 </p> 779 780 <p></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 @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"> </h2> 809 810 <p> 811 </p> 812 <ul> 813 <li>{@link android.view.Menu#setGroupVisible(int,boolean) 814 setGroupVisible()} </li> 815 <li>{@link android.view.Menu#setGroupEnabled(int,boolean) 816 setGroupEnabled()} </li> 817 <li>{@link 818 android.view.Menu#setGroupCheckable(int,boolean,boolean) setGroupCheckable()} </li> 819 </ul> 820 821 <p> {@code <group>} {@code <item>} {@link 822 android.view.Menu#add(int,int,int,int) add()} ID 823 </p> 824 825 <p> </p> 826 827 <pre> 828 <?xml version="1.0" encoding="utf-8"?> 829 <menu xmlns:android="http://schemas.android.com/apk/res/android"> 830 <item android:id="@+id/menu_save" 831 android:icon="@drawable/menu_save" 832 android:title="@string/menu_save" /> 833 <!-- menu group --> 834 <group android:id="@+id/group_delete"> 835 <item android:id="@+id/menu_archive" 836 android:title="@string/menu_archive" /> 837 <item android:id="@+id/menu_delete" 838 android:title="@string/menu_delete" /> 839 </group> 840 </menu> 841 </pre> 842 843 <p> 3 844 ID 2 845 846 {@code 847 android:showAsAction="ifRoom"} 848 </p> 849 850 851 <h3 id="checkable"> </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> 5.</strong> 856 </p> 857 </div> 858 859 <p> 860 861 5 862 </p> 863 864 <p class="note"><strong>:</strong> 865 866 867 </p> 868 869 <p> {@code <item>} {@code 870 android:checkable} {@code <group>} {@code android:checkableBehavior} 871 872 </p> 873 874 <pre> 875 <?xml version="1.0" encoding="utf-8"?> 876 <menu xmlns:android="http://schemas.android.com/apk/res/android"> 877 <group android:checkableBehavior="single"> 878 <item android:id="@+id/red" 879 android:title="@string/red" /> 880 <item android:id="@+id/blue" 881 android:title="@string/blue" /> 882 </group> 883 </menu> 884 </pre> 885 886 <p>{@code android:checkableBehavior} 887 <dl> 888 <dt>{@code single}</dt> 889 <dd> 1 </dd> 890 <dt>{@code all}</dt> 891 <dd></dd> 892 <dt>{@code none}</dt> 893 <dd></dd> 894 </dl> 895 896 <p>{@code <item>} {@code android:checked} {@link 897 android.view.MenuItem#setChecked(boolean) setChecked()} 898 </p> 899 900 <p> {@link android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} item-selected 901 902 903 {@link android.view.MenuItem#isChecked()} {@link android.view.MenuItem#setChecked(boolean) setChecked()} 904 905 </p> 906 907 <pre> 908 @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> 923 924 925 926 </p> 927 928 <p class="note"><strong>:</strong> 929 930 <a href="{@docRoot}guide/topics/data/data-storage.html#pref"></a> 931 </p> 932 933 934 935 <h2 id="intents"> </h2> 936 937 <p>{@link android.content.Intent} 938 on-item-selected {@link 939 android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} {@link android.app.Activity#startActivity(Intent) startActivity()} 940 941 942 </p> 943 944 <p> 945 946 947 Android Android 948 </p> 949 950 <p> : </p> 951 <ol> 952 <li>{@link android.content.Intent#CATEGORY_ALTERNATIVE} {@link android.content.Intent#CATEGORY_SELECTED_ALTERNATIVE} 953 954 </li> 955 <li>{@link 956 android.view.Menu#addIntentOptions(int,int,int,ComponentName,Intent[],Intent,int,MenuItem[]) 957 Menu.addIntentOptions()} Android 958 </li> 959 </ol> 960 961 <p> 962 </p> 963 964 <p class="note"><strong>:</strong> {@link android.content.Intent#CATEGORY_SELECTED_ALTERNATIVE} 965 966 {@link 967 android.app.Activity#onCreateContextMenu(ContextMenu,View,ContextMenuInfo) 968 onCreateContextMenu()} </p> 969 970 <p></p> 971 972 <pre> 973 @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> <code>android:label</code> 997 998 {@link android.view.Menu#addIntentOptions(int,int,int,ComponentName,Intent[],Intent,int,MenuItem[]) 999 addIntentOptions()} 1000 </p> 1001 1002 <p class="note"><strong>:</strong> {@link 1003 android.view.Menu#addIntentOptions(int,int,int,ComponentName,Intent[],Intent,int,MenuItem[]) 1004 addIntentOptions()} 1005 </p> 1006 1007 1008 <h3 id="AllowingToAdd"></h3> 1009 1010 <p> 1011 </p> 1012 1013 <p> {@link android.content.Intent#CATEGORY_ALTERNATIVE} {@link android.content.Intent#CATEGORY_SELECTED_ALTERNATIVE} 1014 1015 1016 </p> 1017 <pre> 1018 <intent-filter label="@string/resize_image"> 1019 ... 1020 <category android:name="android.intent.category.ALTERNATIVE" /> 1021 <category android:name="android.intent.category.SELECTED_ALTERNATIVE" /> 1022 ... 1023 </intent-filter> 1024 </pre> 1025 1026 <p> <a href="/guide/components/intents-filters.html"> </a> 1027 </p> 1028 1029 <p> <a href="{@docRoot}resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html">Note Pad</a> 1030 1031 </p> 1032