A contextual menu offers actions that affect a specific item or context frame in the UI. You can provide a context menu for any view, but they are most often used for items in a ListView, GridView, or other view collections in which the user can perform direct actions on each item.
一個操作功能表提供了行動,影響特定項目或上下文架構在UI。你可以提供一個操作功能表,任何觀點,但他們通常都用於項目在列表視圖中,顯示資料表格,或其他視圖集合中,使用者可以直接在每個項目上執行操作。
There are two ways to provide contextual actions:
有兩種方法提供相關操作:
In a floating context menu. A menu appears as a floating list of menu items (similar to a dialog) when the user performs a long-click (press and hold) on a view that declares support for a context menu. Users can perform a contextual action on one item at a time.
In the contextual action mode. This mode is a system implementation ofActionMode that displays a contextual action bar at the top of the screen with action items that affect the selected item(s). When this mode is active, users can perform an action on multiple items at once (if your app allows it).
Note: The contextual action mode is available on Android 3.0 (API level 11) and higher and is the preferred technique for displaying contextual actions when available. If your app supports versions lower than 3.0 then you should fall back to a floating context menu on those devices.
在一個浮動的操作功能表。一個菜單顯示為一個浮動功能表項目列表(類似於一個對話方塊),在使用者執行一個長點擊(按下並保持住)在一個視圖,宣布支援一個操作功能表。使用者可以執行內容動作在一個項目在一個時間。 在上下文動作模式。這種模式是一個系統實現的ActionMode上下文操作欄顯示在螢幕頂端與行動項目,影響選擇的項目(s)。當這個模式被啟用時,使用者可以執行一個動作在多重專案在一次(如果你的應用程式允許它)。 注意:上下文動作模式是可以在Android 3.0(API層級11)和更高的和是首選的技術顯示相關操作可用時。如果你的應用程式支援版本低於3.0,那麼你應該回退到一個浮動的操作功能表在那些裝置。
Creating a floating context menu建立一個浮動的操作功能表
To provide a floating context menu:
Register the View to which the context menu should be associated by calling registerForContextMenu() and pass it the View.
If your activity uses a ListView or GridView and you want each item to provide the same context menu, register all items for a context menu by passing the ListView or GridView to registerForContextMenu().
Implement the onCreateContextMenu() method in your Activity or Fragment.
When the registered view receives a long-click event, the system calls your onCreateContextMenu() method. This is where you define the menu items, usually by inflating a menu resource. For example:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}MenuInflater allows you to inflate the context menu from a menu resource. The callback method parameters include the View that the user selected and a ContextMenu.ContextMenuInfo object that provides additional information about the item selected. If your activity has several views that each provide a different context menu, you might use these parameters to determine which context menu to inflate.
Implement onContextItemSelected().
When the user selects a menu item, the system calls this method so you can perform the appropriate action. For example:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}The getItemId() method queries the ID for the selected menu item, which you should assign to each menu item in XML using the android:id attribute, as shown in the section about Defining a Menu in XML.
When you successfully handle a menu item, return true. If you don't handle the menu item, you should pass the menu item to the superclass(超類) implementation. If your activity includes fragments, the activity receives this callback first. By calling the superclass when unhandled, the system passes the event to the respective callback method in each fragment, one at a time (in the order each fragment was added) until trueor false is returned. (The default implementation for Activity and android.app.Fragment return false, so you should always call the superclass(超類) when unhandled.)