Creating Contextual Menus创建上下文菜单

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 aListView,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 afloating 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 thecontextual action mode. This mode is a system implementation ofActionModethat displays acontextual action barat 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:

  1. Register theViewto which the context menu should be associated by callingregisterForContextMenu()and pass it theView.

    If your activity uses aListVieworGridViewand you want each item to provide the same context menu, register all items for a context menu by passing theListVieworGridViewtoregisterForContextMenu().

  2. Implement theonCreateContextMenu()method in yourActivityorFragment.

    When the registered view receives a long-click event, the system calls youronCreateContextMenu()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);
    }

    MenuInflaterallows you to inflate the context menu from amenu resource. The callback method parameters include theViewthat the user selected and aContextMenu.ContextMenuInfoobject 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.

  3. ImplementonContextItemSelected().

    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);
      }
    }

    ThegetItemId()method queries the ID for the selected menu item, which you should assign to each menu item in XML using theandroid:idattribute, as shown in the section aboutDefining a Menu in XML.

    When you successfully handle a menu item, returntrue. If you don't handle the menu item, you should pass the menu item to thesuperclass(超类)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) untiltrueorfalseis returned. (The default implementation forActivityandandroid.app.Fragmentreturnfalse, so you should always call thesuperclass(超类)when unhandled.)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值