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 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 of
ActionMode
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 callingregisterForContextMenu()
and pass it theView
.If your activity uses a
ListView
orGridView
and you want each item to provide the same context menu, register all items for a context menu by passing theListView
orGridView
toregisterForContextMenu()
. - Implement the
onCreateContextMenu()
method in yourActivity
orFragment
.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 theView
that the user selected and aContextMenu.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:
@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 theandroid: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) untiltrue
orfalse
is returned. (The default implementation forActivity
andandroid.app.Fragment
returnfalse
, so you should always call the superclass(超类) when unhandled.)
Creating Contextual Menus创建上下文菜单的更多相关文章
- Creating Context Menu / 创建上下文菜单项 / VC++, Windows, DLL, ATL, COM
创建上下文菜单项 1.新建一个ATL Project. 2.建议将Project Property中Linker – General - “Register Output” 设为no,C/C++ - ...
- Android Dialog 创建上下文菜单
Android Dialog中的listview创建上下文菜单 listView.setOnCreateContextMenuListener(new OnCreateContextMenuListe ...
- ContextMenu菜单创建 上下文菜单的基本认识q
MainActivity.class public class MainActivity extends AppCompatActivity { @Override protected void on ...
- android 开发-(Contextual Menu)上下文菜单的实现
在android3.0以后,安卓设备不在提供物理的菜单按键,同时,android应用提供了另外的菜单实现机制,来替代之前的菜单创建方式.安卓设备中,平常可以使用长按住某个内容弹出菜单选项.这就是我们需 ...
- 【WP 8.1开发】上下文菜单
在桌面系统中,别说是开发者,相信有资格考得过计算机一级的人都知道什么叫一下文菜单,或者叫右键菜单. 为了让操作更方便,在手机应用程序中,也应当有这样的菜单.上下文菜单之所以有”上下文“之说,是因为通常 ...
- 安卓开发_浅谈ContextMenu(上下文菜单)
长下文菜单,即长按view显示一个菜单栏 与OptionMenu的区别OptionMenu对应的是activity,一个activity只能拥有一个选项菜单ContextMenu对应的是View,每个 ...
- android 上下文菜单详解
本文使用xml来创建上下文菜单 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:andr ...
- android项目--上下文菜单
一般说到上下文菜单基本上都是长按事件,在一个控件上长按,就会弹出一个菜单. 1.创建上下文菜单: //覆盖方法,创建上下文菜单 @Override public void onCreateContex ...
- Android开发 ---xml构建选项菜单、上下文菜单(长按显示菜单)、发通知、发送下载通知
1.activity_main.xml 描述: 定义了一个TextView和三个按钮 <?xml version="1.0" encoding="utf-8&quo ...
随机推荐
- boost::asio设置同步连接超时
boost::asio设置同步连接超时 CSDN上求助无果,只好用自创的非主流方法了.asio自带的例子里是用deadline_timer的async_wait方法来实现超时的,这种方法需要单独写 ...
- 关于yaf的控制器命名,一个纠结的问题(续)
以下方案缺少loader相关的步骤,明天补上!!! 前面写过一篇<关于yaf的控制器命名,一个纠结的问题>.没想到yaf群里面也有跟我遇到一样问题的人,分享下解决办法. 写完那篇博文后,我 ...
- Codeforces 396B On Sum of Fractions 数论
题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...
- ios添加pre和post build action
再vs中,我们可以很方便的再build前.后执行一些脚本为我们做点什么事情.再ios中怎么搞呢,哪必然是对xcode进行操作了.再google搜索了一把,有说操作Scheme的也有说再直接再targe ...
- linux shell编程指南第十八章------控制流结构
在书写正确脚本前,大概讲一下退出状态.任何命令进行时都将返回一个退出状态.如 果要观察其退出状态,使用最后状态命令: $ echo $? 主要有4种退出状态.前面已经讲到了两种,即最后命令退出状态$ ...
- flex中在basewidget中不能使用图表组件问题
参考 http://blog.sina.com.cn/s/blog_51e3d0e70101hljz.html
- 重复数据删除(De-duplication)技术研究(SourceForge上发布dedup util)
dedup util是一款开源的轻量级文件打包工具,它基于块级的重复数据删除技术,可以有效缩减数据容量,节省用户存储空间.目前已经在Sourceforge上创建项目,并且源码正在不断更新中.该工具生成 ...
- 使用ThinkPHP+Uploadify实现图片上传功能
首先,将下载的Uploadify压缩包解压放到公共文件夹内.实现代码如下: 前台html部分: <script src="/uploadify/jquery.min.js" ...
- jsp中将后台传递过来的json格式的list数据绑定到下拉菜单select
<span style="white-space:pre"> </span> <select><c:forEach var="f ...
- centos6安装bt工具transmission
centos6 install transmission 1. 安装所需的组件: yum -y install gcc gcc-c++ m4 make automake libtool gettex ...