Main Menu Game Là Gì Và 5 Bí Mật Thú Vị Bạn Cần Biết Về Nó! &Ndash Nshop

Menus are a common user interface component in many types of applications. To provide a familiarand consistent user experience, you should use the Menu APIs to present useractions and other options in your activities.

Đang xem: Menu game là gì

Xem thêm: Menu game là gì

Beginning with tienkiem.com.vn 3.0 (API level 11), tienkiem.com.vn-powered devices are no longer required toprovide a dedicated Menu button. With this change, tienkiem.com.vn apps should migrate away from adependence on the traditional 6-item menu panel and instead provide an app bar to present commonuser actions.

Although the design and user experience for some menu items have changed, the semantics to definea set of actions and options is still based on the Menu APIs. Thisguide shows how to create the three fundamental types of menus or action presentations on allversions of tienkiem.com.vn:

Options menu and app bar The options menu is the primary collection of menu items for anactivity. It”s where you should place actions that have a global impact on the app, such as”Search,” “Compose email,” and “Settings.”

See the section about Creating an Options Menu.

Context menu and contextual action mode A context menu is a floating menu that appears when theuser performs a long-click on an element. It provides actions that affect the selected content orcontext frame.

The contextual action mode displaysaction items that affect the selected content in a bar at the top of the screen and allows the userto select multiple items.

See the section about Creating Contextual Menus.

Popup menu A popup menu displays a list of items in a vertical list that”s anchored to the view thatinvoked the menu. It”s good for providing an overflow of actions that relate to specific content orto provide options for a second part of a command. Actions in a popup menu shouldnot directly affect the corresponding content—that”s what contextual actionsare for. Rather, the popup menu is for extended actions that relate to regions of content in youractivity.

See the section about Creating a Popup Menu.

Defining a Menu in XML

For all menu types, tienkiem.com.vn provides a standard XML format to define menu items.Instead of building a menu in your activity”s code, you should define a menu and all its items in anXML menu resource. You can theninflate the menu resource (load it as a Menu object) in your activity orfragment.

Using a menu resource is a good practice for a few reasons:

It”s easier to visualize the menu structure in XML. It separates the content for the menu from your application”s behavioral code.

To define the menu, create an XML file inside your project”s res/menu/directory and build the menu with the following elements:

Defines a Menu, which is a container for menu items. A element must be the root node for the file and can hold one or more and elements. Creates a MenuItem, which represents a single item in a menu. Thiselement may contain a nested element in order to create a submenu. An optional, invisible container for elements. It allows you tocategorize menu items so they share properties such as active state and visibility. For moreinformation, see the section about Creating Menu Groups.

Here”s an example menu named game_menu.xml:

The element supports several attributes you can use to define an item”sappearance and behavior. The items in the above menu include the following attributes:

tienkiem.com.vn:id A resource ID that”s unique to the item, which allows the application to recognize the itemwhen the user selects it. tienkiem.com.vn:icon A reference to a drawable to use as the item”s icon. tienkiem.com.vn:title A reference to a string to use as the item”s title. tienkiem.com.vn:showAsAction Specifies when and how this item should appear as an action item in the app bar.

These are the most important attributes you should use, but there are many more available.For information about all the supported attributes, see the Menu Resource document.

You can add a submenu to an item in any menu by adding a element as the child of an . Submenus are useful when your application has a lotof functions that can be organized into topics, like items in a PC application”s menu bar (File,Edit, View, etc.). For example:

To use the menu in your activity, you need to inflate the menu resource (convert the XMLresource into a programmable object) using MenuInflater.inflate(). In the following sections, you”ll see how to inflate a menu for eachmenu type.

Creating an Options Menu

*

Figure 1. Options menu in theBrowser.

The options menu is where you should include actions and other options that are relevant to thecurrent activity context, such as “Search,” “Compose email,” and “Settings.”

Where the items in your options menu appear on the screen depends on the version for which you”vedeveloped your application:

If you”ve developed your application for tienkiem.com.vn 2.3.x (API level 10) orlower, the contents of your options menu appear at the top of the screen when the userpresses the Menu button, as shown in figure 1. When opened, the first visible portion isthe iconmenu, which holds up to six menu items. If your menu includes more than six items, tienkiem.com.vn placesthe sixth item and the rest into the overflow menu, which the user can open by selectingMore.

*

Figure 2. The Google Sheets app,showing several buttons, including the action overflow button.

You can declare items for the options menu from either your Activitysubclass or a Fragment subclass. If both your activity and fragment(s)declare items for the options menu, they are combined in the UI. The activity”s items appearfirst, followed by those of each fragment in the order in which each fragment is added to theactivity. If necessary, you can re-order the menu items with the tienkiem.com.vn:orderInCategoryattribute in each you need to move.

To specify the options menu for an activity, override onCreateOptionsMenu() (fragments provide theirown onCreateOptionsMenu() callback). In thismethod, you can inflate your menu resource (defined in XML) into the Menu provided in the callback. For example:

Xem thêm:  Dual Rank là gì? Các từ ngữ liên quan đến Rank trong game

Kotlin

override fun onCreateOptionsMenu(menu: Menu): Boolean { val inflater: MenuInflater = menuInflater inflater.inflate(R.menu.game_menu, menu) return true}

Java

Overridepublic boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true;}You can also add menu items using add() and retrieve items with findItem() to revise theirproperties with MenuItem APIs.

If you”ve developed your application for tienkiem.com.vn 2.3.x and lower, the system calls onCreateOptionsMenu() to create the options menuwhen the user opens the menu for the first time. If you”ve developed for tienkiem.com.vn 3.0 and higher,the system calls onCreateOptionsMenu() whenstarting the activity, in order to show items to the app bar.

Handling click events

When the user selects an item from the options menu (including action items in the app bar),the system calls your activity”s onOptionsItemSelected() method. This method passes the MenuItem selected. Youcan identify the item by calling getItemId(), which returns the uniqueID for the menu item (defined by the tienkiem.com.vn:id attribute in the menu resource or with aninteger given to the add() method). You can matchthis ID against known menu items to perform the appropriate action. For example:

Kotlin

override fun onOptionsItemSelected(item: MenuItem): Boolean { // Handle item selection return when (item.itemId) { R.id.new_game -> { newGame() true } R.id.help -> { showHelp() true } else -> super.onOptionsItemSelected(item) }}

Java

Overridepublic boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.new_game: newGame(); return true; case R.id.help: showHelp(); return true; default: return super.onOptionsItemSelected(item); }}When you successfully handle a menu item, return true. If you don”t handle the menuitem, you should call the superclass implementation of onOptionsItemSelected() (the defaultimplementation returns false).

If your activity includes fragments, the system first calls onOptionsItemSelected() for the activity thenfor each fragment (in the order each fragment was added) until one returnstrue or all fragments have been called.

Tham khảo thêm: MVP là gì? Viết tắt của từ nào? Ý nghĩa của từ MVP

Tip: tienkiem.com.vn 3.0 adds the ability for you to define the on-clickbehavior for a menu item in XML, using the tienkiem.com.vn:onClick attribute. The value for theattribute must be the name of a method defined by the activity using the menu. The methodmust be public and accept a single MenuItem parameter—when the systemcalls this method, it passes the menu item selected. For more information and an example, see the Menu Resource document.

Tip: If your application contains multiple activities andsome of them provide the same options menu, consider creatingan activity that implements nothing except the onCreateOptionsMenu() and onOptionsItemSelected() methods. Then extend this class for each activity that should share thesame options menu. This way, you can manage one set of code for handling menuactions and each descendant class inherits the menu behaviors.If you want to add menu items to one of the descendant activities,override onCreateOptionsMenu() in that activity. Call super.onCreateOptionsMenu(menu) so theoriginal menu items are created, then add new menu items with menu.add(). You can also override the super class”sbehavior for individual menu items.

Changing menu items at runtime

After the system calls onCreateOptionsMenu(), it retains an instance of the Menu you populate andwill not call onCreateOptionsMenu()again unless the menu is invalidated for some reason. However, you should use onCreateOptionsMenu() only to create the initialmenu state and not to make changes during the activity lifecycle.

If you want to modify the options menu based onevents that occur during the activity lifecycle, you can do so inthe onPrepareOptionsMenu() method. Thismethod passes you the Menu object as it currently exists so you can modify it,such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)

On tienkiem.com.vn 2.3.x and lower, the system calls onPrepareOptionsMenu() each time the user opens the options menu (presses the Menubutton).

On tienkiem.com.vn 3.0 and higher, the options menu is considered to always be open when menu items arepresented in the app bar. When an event occurs and you want to perform a menu update, you mustcall invalidateOptionsMenu() to request that thesystem call onPrepareOptionsMenu().

Note:You should never change items in the options menu based on the View currentlyin focus. When in touch mode (when the user is not using a trackball or d-pad), viewscannot take focus, so you should never use focus as the basis for modifyingitems in the options menu. If you want to provide menu items that are context-sensitive to a View, use a Context Menu.

Creating Contextual Menus

*

Figure 3. Screenshots of a floating context menu (left)and the contextual action bar (right).

A contextual menu offers actions that affect a specific item or context frame in the UI. Youcan provide a context menu for any view, but they are most often used for items in a ListView, GridView, or other view collections in whichthe user can perform direct actions on each item.

There are two ways to provide contextual actions:

Note: The contextual action mode is available on tienkiem.com.vn 3.0 (APIlevel 11) and higher and is the preferred technique for displaying contextual actions whenavailable. If your app supports versions lower than 3.0 then you should fall back to a floatingcontext menu on those devices.

Xem thêm: Game Bản Repack Là Gì – Phần Mềm Repack Là Gì

Creating a floating context menu

To provide a floating context menu:

Using the contextual action mode

The contextual action mode is a system implementation of ActionMode thatfocuses user interaction toward performing contextual actions. When auser enables this mode by selecting an item, a contextual action bar appears at the top ofthe screen to present actions the user can perform on the currently selected item(s). While thismode is enabled, the user can select multiple items (if you allow it), deselect items, and continueto navigate within the activity (as much as you”re willing to allow). The action mode is disabledand the contextual action bar disappears when the user deselects all items, presses the BACK button,or selects the Done action on the left side of the bar.

Note: The contextual action bar is not necessarilyassociated with the app bar. They operateindependently, even though the contextual action bar visually overtakes the app barposition.

For views that provide contextual actions, you should usually invoke the contextual action modeupon one of two events (or both):

Xem thêm:  Minecraft là gì? Siêu phẩm game thu hút triệu người chơi

The user performs a long-click on the view. The user selects a checkbox or similar UI component within the view.

How your application invokes the contextual action mode and defines the behavior for eachaction depends on your design. There are basically two designs:

For contextual actions on individual, arbitrary views.

The following sections describe the setup required for each scenario.

Enabling the contextual action mode for individual views

If you want to invoke the contextual action mode only when the user selects specificviews, you should:

For example:

Enabling batch contextual actions in a ListView or GridView

If you have a collection of items in a ListView or GridView (or another extension of AbsListView) and want toallow users to perform batch actions, you should:

For example:

Kotlin

val listView: ListView = getListView()with(listView) { choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL setMultiChoiceModeListener(object : AbsListView.MultiChoiceModeListener { override fun onItemCheckedStateChanged(mode: ActionMode, position: Int, id: Long, checked: Boolean) { // Here you can do something when items are selected/de-selected, // such as update the title in the CAB } override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { // Respond to clicks on the actions in the CAB return when (item.itemId) { R.id.menu_delete -> { deleteSelectedItems() mode.finish() // Action picked, so close the CAB true } else -> false } } override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { // Inflate the menu for the CAB val menuInflater: MenuInflater = mode.menuInflater menuInflater.inflate(R.menu.context, menu) return true } override fun onDestroyActionMode(mode: ActionMode) { // Here you can make any necessary updates to the activity when // the CAB is removed. By default, selected items are deselected/unchecked. } override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean { // Here you can perform updates to the CAB due to // an invalidate() request return false } })}

Java

ListView listView = getListView();listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);listView.setMultiChoiceModeListener(new MultiChoiceModeListener() { Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { // Here you can do something when items are selected/de-selected, // such as update the title in the CAB } Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { // Respond to clicks on the actions in the CAB switch (item.getItemId()) { case R.id.menu_delete: deleteSelectedItems(); mode.finish(); // Action picked, so close the CAB return true; default: return false; } } Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { // Inflate the menu for the CAB MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.context, menu); return true; } Override public void onDestroyActionMode(ActionMode mode) { // Here you can make any necessary updates to the activity when // the CAB is removed. By default, selected items are deselected/unchecked. } Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { // Here you can perform updates to the CAB due to // an invalidate() request return false; }});That”s it. Now when the user selects an item with a long-click, the system calls the onCreateActionMode()method and displays the contextual action bar with the specified actions. While the contextualaction bar is visible, users can select additional items.

In some cases in which the contextual actions provide common action items, you mightwant to add a checkbox or a similar UI element that allows users to select items, because theymight not discover the long-click behavior. When a user selects the checkbox, youcan invoke the contextual action mode by setting the respective list item to the checkedstate with setItemChecked().

Creating a Popup Menu

*

Figure 4. A popup menu in the Gmail app, anchored to the overflowbutton at the top-right.

A PopupMenu is a modal menu anchored to a View.It appears below the anchor view if there is room, or above the view otherwise. It”s useful for:

Providing a second part of a command sentence (such as a button marked “Add”that produces a popup menu with different “Add” options).

Note: PopupMenu is available with APIlevel 11 and higher.

If you define your menu in XML, here”s how you can show the popup menu:

For example, here”s a button with the tienkiem.com.vn:onClick attributethat shows a popup menu:

The activity can then show the popup menu like this:

Kotlin

Đọc thêm: Game Match 3 là gì? 5 tựa game Match 3 mà bạn nên thử

fun showPopup(v: View) { val popup = PopupMenu(this, v) val inflater: MenuInflater = popup.menuInflater inflater.inflate(R.menu.actions, popup.menu) popup.show()}

Java

public void showPopup(View v) { PopupMenu popup = new PopupMenu(this, v); MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.actions, popup.getMenu()); popup.show();}In API level 14 and higher, you can combine the two lines that inflate the menu with PopupMenu.inflate().

The menu is dismissed when the user selects an item or touches outside the menuarea. You can listen for the dismiss event using PopupMenu.OnDismissListener.

Handling click events

To perform anaction when the user selects a menu item, you must implement the PopupMenu.OnMenuItemClickListener interface and register it with your PopupMenu by calling setOnMenuItemclickListener(). When the user selects an item, the system calls the onMenuItemClick() callback inyour interface.

For example:

Kotlin

fun showMenu(v: View) { PopupMenu(this, v).apply { // MainActivity implements OnMenuItemClickListener setOnMenuItemClickListener(thisMainActivity) inflate(R.menu.actions) show() }}override fun onMenuItemClick(item: MenuItem): Boolean { return when (item.itemId) { R.id.archive -> { archive(item) true } R.id.delete -> { delete(item) true } else -> false }}

Java

public void showMenu(View v) { PopupMenu popup = new PopupMenu(this, v); // This activity implements OnMenuItemClickListener popup.setOnMenuItemClickListener(this); popup.inflate(R.menu.actions); popup.show();}Overridepublic boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.archive: archive(item); return true; case R.id.delete: delete(item); return true; default: return false; }}

Creating Menu Groups

A menu group is a collection of menu items that share certain traits. With a group, youcan:

You can create a group by nesting elements inside a element in your menu resource or by specifying a group ID with the add() method.

Here”s an example menu resource that includes a group:

The items that are in the group appear at the same level as the first item—all three itemsin the menu are siblings. However, you can modify the traits of the twoitems in the group by referencing the group ID and using the methods listed above. The systemwill also never separate grouped items. For example, if you declare tienkiem.com.vn:showAsAction=”ifRoom” for each item, they will either both appear in the actionbar or both appear in the action overflow.

Using checkable menu items

*

Figure 5. Screenshot of a submenu with checkableitems.

A menu can be useful as an interface for turning options on and off, using a checkbox forstand-alone options, or radio buttons for groups ofmutually exclusive options. Figure 5 shows a submenu with items that are checkable with radiobuttons.

Note: Menu items in the Icon Menu (from the options menu) cannotdisplay a checkbox or radio button. If you choose to make items in the Icon Menu checkable,you must manually indicate the checked state by swapping the icon and/or texteach time the state changes.

Xem thêm:  Cách chơi Akali Tốc Chiến | Bảng ngọc, lên đồ và combo chuẩn

You can define the checkable behavior for individual menu items using the tienkiem.com.vn:checkable attribute in the element, or for an entire group withthe tienkiem.com.vn:checkableBehavior attribute in the element. Forexample, all items in this menu group are checkable with a radio button:

The tienkiem.com.vn:checkableBehavior attribute accepts either: single Only one item from the group can be checked (radio buttons) all All items can be checked (checkboxes) none No items are checkableYou can apply a default checked state to an item using the tienkiem.com.vn:checked attribute inthe element and change it in code with the setChecked() method.

When a checkable item is selected, the system calls your respective item-selected callback method(such as onOptionsItemSelected()). Itis here that you must set the state of the checkbox, because a checkbox or radio button does notchange its state automatically. You can query the current state of the item (as it was before theuser selected it) with isChecked() and then set the checked state withsetChecked(). For example:

Kotlin

override fun onOptionsItemSelected(item: MenuItem): Boolean { return when (item.itemId) { R.id.vibrate, R.id.dont_vibrate -> { item.isChecked = !item.isChecked true } else -> super.onOptionsItemSelected(item) }}

Java

Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.vibrate: case R.id.dont_vibrate: if (item.isChecked()) item.setChecked(false); else item.setChecked(true); return true; default: return super.onOptionsItemSelected(item); }}If you don”t set the checked state this way, then the visible state of the item (the checkbox orradio button) will notchange when the user selects it. When you do set the state, the activity preserves the checked stateof the item so that when the user opens the menu later, the checked state that youset is visible.

Note:Checkable menu items are intended to be used only on a per-session basis and not saved after theapplication is destroyed. If you have application settings that you would like to save for the user,you should store the data using Shared Preferences.

Adding Menu Items Based on an Intent

Sometimes you”ll want a menu item to launch an activity using an Intent(whether it”s an activity in your application or another application). When you know the intent youwant to use and have a specific menu item that should initiate the intent, you can execute theintent with startActivity() during theappropriate on-item-selected callback method (such as the onOptionsItemSelected() callback).

However, if you are not certain that the user”s devicecontains an application that handles the intent, then adding a menu item that invokes it can resultin a non-functioning menu item, because the intent might not resolve to anactivity. To solve this, tienkiem.com.vn lets you dynamically add menu items to your menuwhen tienkiem.com.vn finds activities on the device that handle your intent.

To add menu items based on available activities that accept an intent:

If there are no applications installedthat satisfy the intent, then no menu items are added.

Note:CATEGORY_SELECTED_ALTERNATIVE is used to handle the currentlyselected element on the screen. So, it should only be used when creating a Menu in onCreateContextMenu().

For example:

Kotlin

override fun onCreateOptionsMenu(menu: Menu): Boolean { super.onCreateOptionsMenu(menu) // Create an Intent that describes the requirements to fulfill, to be included // in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE. val intent = Intent(null, dataUri).apply { addCategory(Intent.CATEGORY_ALTERNATIVE) } // Search and populate the menu with acceptable offering applications. menu.addIntentOptions( R.id.intent_group, // Menu group to which new items will be added 0, // Unique item ID (none) 0, // Order for the items (none) this.componentName, // The current activity name null, // Specific items to place first (none) intent, // Intent created above that describes our requirements 0, // Additional flags to control items (none) null) // Array of MenuItems that correlate to specific items (none) return true}

Java

Overridepublic boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); // Create an Intent that describes the requirements to fulfill, to be included // in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE. Intent intent = new Intent(null, dataUri); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); // Search and populate the menu with acceptable offering applications. menu.addIntentOptions( R.id.intent_group, // Menu group to which new items will be added 0, // Unique item ID (none) 0, // Order for the items (none) this.getComponentName(), // The current activity name null, // Specific items to place first (none) intent, // Intent created above that describes our requirements 0, // Additional flags to control items (none) null); // Array of MenuItems that correlate to specific items (none) return true;}For each activity found that provides an intent filter matching the intent defined, a menuitem is added, using the value in the intent filter”s tienkiem.com.vn:label as themenu item title and the application icon as the menu item icon. TheaddIntentOptions() method returns the number of menu items added.

Note: When you call addIntentOptions(), it overrides any and all menu items by the menu group specified in the firstargument.

Allowing your activity to be added to other menus

You can also offer the services of your activity to other applications, so yourapplication can be included in the menu of others (reverse the roles described above).

To be included in other application menus, you need to define an intentfilter as usual, but be sure to include the CATEGORY_ALTERNATIVEand/or CATEGORY_SELECTED_ALTERNATIVE values for the intent filtercategory. For example:

… …Read more about writing intent filters in theIntents and Intent Filters document.

Xem thêm: Cách Khắc Chế Leblanc – Hướng Dẫn Di Mid Hiệu Quả Nhất

<{ “type”: “thumb-down”, “id”: “missingTheInformationINeed”, “label”:”Missing the information I need” },{ “type”: “thumb-down”, “id”: “tooComplicatedTooManySteps”, “label”:”Too complicated / too many steps” },{ “type”: “thumb-down”, “id”: “outOfDate”, “label”:”Out of date” },{ “type”: “thumb-down”, “id”: “samplesCodeIssue”, “label”:”Samples/Code issue” },{ “type”: “thumb-down”, “id”: “otherDown”, “label”:”Other” }> <{ “type”: “thumb-up”, “id”: “easyToUnderstand”, “label”:”Easy to understand” },{ “type”: “thumb-up”, “id”: “solvedMyProblem”, “label”:”Solved my problem” },{ “type”: “thumb-up”, “id”: “otherUp”, “label”:”Other” }>

Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.

More tienkiem.com.vn

Support

Documentation

*

Language English Bahasa Indonesia Español – América Latina Português – Brasil 中文 – 简体 日本語 한국어

Tham khảo thêm: Flag Trong Game Là Gì ? Tổng Hợp Các Định Nghĩa Thường Gặp Khi Xem Anime

Rate this post