Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Creating Contextual Menus創建上下文菜單

Creating Contextual Menus創建上下文菜單

編輯:關於Android編程

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.)

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved