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 ListViewGridView, 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:

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

  2. 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:

    @Overridepublicvoid 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.

  3. Implement onContextItemSelected().

    When the user selects a menu item, the system calls this method so you can perform the appropriate action. For example:

    @Overridepublicboolean onContextItemSelected(MenuItem item){
        AdapterContextMenuInfo info =(AdapterContextMenuInfo) item.getMenuInfo();
        switch(item.getItemId()){
            case R.id.edit:
                editNote(info.id);
                returntrue;
            case R.id.delete:
                deleteNote(info.id);
                returntrue;
            default:
                returnsuper.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.)

Creating Contextual Menus创建上下文菜单的更多相关文章

  1. Creating Context Menu / 创建上下文菜单项 / VC++, Windows, DLL, ATL, COM

    创建上下文菜单项 1.新建一个ATL Project. 2.建议将Project Property中Linker – General - “Register Output” 设为no,C/C++ - ...

  2. Android Dialog 创建上下文菜单

    Android Dialog中的listview创建上下文菜单 listView.setOnCreateContextMenuListener(new OnCreateContextMenuListe ...

  3. ContextMenu菜单创建 上下文菜单的基本认识q

    MainActivity.class public class MainActivity extends AppCompatActivity { @Override protected void on ...

  4. android 开发-(Contextual Menu)上下文菜单的实现

    在android3.0以后,安卓设备不在提供物理的菜单按键,同时,android应用提供了另外的菜单实现机制,来替代之前的菜单创建方式.安卓设备中,平常可以使用长按住某个内容弹出菜单选项.这就是我们需 ...

  5. 【WP 8.1开发】上下文菜单

    在桌面系统中,别说是开发者,相信有资格考得过计算机一级的人都知道什么叫一下文菜单,或者叫右键菜单. 为了让操作更方便,在手机应用程序中,也应当有这样的菜单.上下文菜单之所以有”上下文“之说,是因为通常 ...

  6. 安卓开发_浅谈ContextMenu(上下文菜单)

    长下文菜单,即长按view显示一个菜单栏 与OptionMenu的区别OptionMenu对应的是activity,一个activity只能拥有一个选项菜单ContextMenu对应的是View,每个 ...

  7. android 上下文菜单详解

    本文使用xml来创建上下文菜单 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:andr ...

  8. android项目--上下文菜单

    一般说到上下文菜单基本上都是长按事件,在一个控件上长按,就会弹出一个菜单. 1.创建上下文菜单: //覆盖方法,创建上下文菜单 @Override public void onCreateContex ...

  9. Android开发 ---xml构建选项菜单、上下文菜单(长按显示菜单)、发通知、发送下载通知

    1.activity_main.xml 描述: 定义了一个TextView和三个按钮 <?xml version="1.0" encoding="utf-8&quo ...

随机推荐

  1. HDU1027 Ignatius and the Princess II 【next_permutation】【DFS】

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  2. UVALive 2519 Radar Installation 雷达扫描 区间选点问题

    题意:在坐标轴中给出n个岛屿的坐标,以及雷达的扫描距离,要求在y=0线上放尽量少的雷达能够覆盖全部岛屿. 很明显的区间选点问题. 代码: /* * Author: illuz <iilluzen ...

  3. Mac 修改Host 绑定host

    Mac 系统下 ,修改Host 文件: 打开命令行终端 输入 sudo vi /etc/hosts 之后回车确认,进入vi 编辑界面(进行vi编辑操作,之后保存就行了) 版权声明:本文为博主原创文章, ...

  4. Android开发者指南-用户界面-拖放-Drag and Drop[原创译文]

      英文原文:http://developer.android.com/guide/topics/ui/drag-drop.html 版本:Android 4.0 r1 译者注:黄色底色为未决译文 快 ...

  5. 在VC资源文件中加入声音资源

    本文介绍如何在VC资源文件中加入自己的声音资源,使自己的应用程序可以播放声音. 1.首先用文本编辑器(如记事本)打开资源文件(.rc文件) 在最后加入自己的声音资源,如下IDW WAVE " ...

  6. Lucene.Net 2.3.1开发介绍 —— 三、索引(四)

    原文:Lucene.Net 2.3.1开发介绍 -- 三.索引(四) 4.索引对搜索排序的影响 搜索的时候,同一个搜索关键字和同一份索引,决定了一个结果,不但决定了结果的集合,也确定了结果的顺序.那个 ...

  7. Goffi and Squary Partition

    题意: 给你N和K,问能否将N拆分成K个互不相同的正整数,并且其中K-1个数的和为完全平方数. PS:这道题目原来是要求输出一种可行方案的,所以下面题解是按照输出方案的思想搞的. 分析: 我们尝试枚举 ...

  8. Android生存指南:Eclipse快捷键

    天天使用的东西,可能是由于太习惯它了吧.总忘记学习怎样高效的使用它.正的谓磨刀不误劈柴功,找时间好好磨磨刀.于人于已都是有优点的.效率高了,多省出点时间去干自己真正喜欢干的事情吧. 精进Eclipse ...

  9. xcode5下一个ffmpeg静态库配置

    1.若要安装xcode命令行工具 1).xcode5安装命令行工具方法: 在终端运行命令Using xcode-select --install 2).xcode5之前安装命令行工具方法: 2.xco ...

  10. callback用法简介

    源地址:https://argcv.com/articles/2669.c callback,函数的回调,从ANSI C开始,一直被广为使用.无论是windows API的所谓消息机制,动态链接库的调 ...