http://blog.csdn.net/kavensu/article/details/8045041

onCreateOptionsMenu :此方法为创建菜单方法,这个菜单就是你在点击手机menu键时会看到的菜单。

onCreateContextMenu :此方法为创建菜单方法,你还需要对此菜单进行注册Activity.registerForContextMenu(View view),这个菜单是在你长按前面注册的view时看到的菜单。

onMenuItemSelected :当你选择上面两种菜单任意一种时都会触发这个事件。

onOptionsItemSelected :这个方法只在onCreateOptionsMenu 创建的菜单被选中时才会被触发。

onContextItemSelected :这个方法只在onCreateContextMenu 创建的菜单被选中时才会被触发。

/*因为在一个Activity中都使用onMenuItemSelected和onContextItemSelected时会冲突,
也就是说,当有长按弹出菜单的时候,点击菜单的选项响应的是onMenuItemSelected(),如果被
点击的Item Id不在 onMenuItemSelected中,事件就不会响应,此时不会再调用onContextItemSelected。
这样的话,我们在onContextItemSelected中定义的事件就不会执行,这就是很多人遇到的“onContextItemSelected失效”的原因.
这个时候,需要在onContextItemSelected做相应的判断,如果执行的事件不在onMenuItemSelected中,就执行onContextItemSelected。
*/

  1. @Override
  2. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  3. switch (item.getItemId()) {
  4. case R.id.about:
  5. Intent intent = new Intent();
  6. intent.setClass(BaseActivity.this, AboutActivity.class);
  7. startActivity(intent);
  8. return true;
  9. case R.id.bgsettings:
  10. Intent changeBgIntent = new Intent();
  11. changeBgIntent.setClass(BaseActivity.this, BgActivity.class);
  12. startActivity(changeBgIntent);
  13. return true;
  14. case R.id.help:
  15. Intent helpIntent = new Intent();
  16. helpIntent.setClass(BaseActivity.this, HelpActivity.class);
  17. startActivity(helpIntent);
  18. return true;
  19. case R.id.exit:
  20. System.exit(0);
  21. return true;
  22. /*因为在一个Activity中都使用onMenuItemSelected和onContextItemSelected时会冲突,
  23. 也就是说,当有长按弹出菜单的时候,点击菜单的选项响应的是onMenuItemSelected(),如果被
  24. 点击的Item Id不在 onMenuItemSelected中,事件就不会响应,此时不会再调用onContextItemSelected。
  25. 这样的话,我们再onContextItemSelected中定义的事件就不会执行,这就是很多人遇到的“onContextItemSelected失效”的原因.
  26. 这个时候,需要在onContextItemSelected做相应的判断,如果执行的事件不在onMenuItemSelected中,就执行onContextItemSelected。
  27. */
  28. default:
  29. onContextItemSelected(item);
  30. }
  31. return false;
  32. }
    1. @Override
    2. public boolean onContextItemSelected(MenuItem item) {
    3. System.out.println("--------onContextItemSelected---------");
    4. switch (item.getItemId()) {
    5. case R.id.collect_book:
    6. doCollect();
    7. return true;
    8. case R.id.detail:
    9. Toast.makeText(this, "书籍详细信息", 3000).show();
    10. return true;
    11. default:
    12. return super.onContextItemSelected(item);
    13. }
    14. }

在点击选项菜单(OptionsMenu:点击menu弹出的菜单)的菜单项时即调用了 onMenuItemSelected 也调用了onOptionsItemSelected ,于是疑惑他们有什么不一样。然后再看上下文菜单(ContextMenu:长按控件时弹出。例如在长按EditTextView时弹出输入的法选择菜 单),点击上下文菜单的菜单项时即调用了onMenuItemSelected 也调用了onContextItemSelected ,于是问题就明朗了。在父Activity的onMenuItemSelected 方法中判断如果是选项菜单就走onMenuItemSelected ,如果是上下文菜单就走onContextItemSelected 。

除此还有

onPrepareOptionsMenu:在显示选项菜单前调用

onOptionsMenuClosed:在选项菜单关闭时被调用

onContextMenuClosed:在上下文菜单关闭时调用

onContextItemSelected 用法的更多相关文章

  1. ConxtMenu高级用法

    ##背景我们经常在列表的页面中,点击列表中的行,一般进入详情页面,长按列表中一行,会弹出一个菜单,包含了对某一行的操作(编辑.删除等等),也知道通常的用法: 0x01. 在Activity中注册需要上 ...

  2. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  3. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  4. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  5. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  6. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  7. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  8. 【JavaScript】innerHTML、innerText和outerHTML的用法区别

    用法: <div id="test">   <span style="color:red">test1</span> tes ...

  9. chattr用法

    [root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...

随机推荐

  1. 【HDU3948】 The Number of Palindromes (后缀数组+RMQ)

    The Number of Palindromes Problem Description Now, you are given a string S. We want to know how man ...

  2. Principles of Motion Sensing

    Principlesof Motion Sensing Various sensors capable of detecting motionin free space have been comme ...

  3. JSch - Java实现的SFTP(文件上传详解篇)(转)

    JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到 ...

  4. Java Random随机种子

    第一种情况 Random rand = new Random(47); for(int i=0;i<10;i++) System.out.println(rand.nextInt(100)); ...

  5. innodb_buffer_pool_instances and innodb_buffer_pool_size的关系

    把buffer pool 分成一个用户指定的单独的区域, 每个有它自己的LRU list和相关的数据结构, 降低竞争在并发内存读取和写操作. 这个选项只有当innodb_buffer_pool_siz ...

  6. [LeetCode#163] Missing Ranges

    Problem: Given a sorted integer array where the range of elements are [lower, upper] inclusive, retu ...

  7. HDU-2509 Be the Winner

    http://acm.hdu.edu.cn/showproblem.php?pid=2509 Be the Winner Time Limit: 2000/1000 MS (Java/Others)  ...

  8. 最详细在Windows安装Xamarin.iOS教程

    最详细在Windows安装Xamarin.iOS教程 来源:http://www.cnblogs.com/llyfe2006/articles/3098280.html 本文展示了如何设立Xamari ...

  9. HDOJ/HDU 2551 竹青遍野(打表~)

    Problem Description "临流揽镜曳双魂 落红逐青裙 依稀往梦幻如真 泪湿千里云" 在MCA山上,除了住着众多武林豪侠之外,还生活着一个低调的世外高人,他本名逐青裙 ...

  10. poj 1050 To the Max (简单dp)

    题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...