安卓中的上下文菜单是通过长按控件元素触发的,要注意的是每次都会触发onCreateContextMenu方法:

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6.  
  7. <TextView
  8. android:id="@+id/txt"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="可通过上下文菜单修改背景色"
  12. android:textSize="15pt" />
  13.  
  14. </LinearLayout>

ContextMenuTest.java

  1. package org.crazyit.ui;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.Color;
  5. import android.os.Bundle;
  6. import android.view.ContextMenu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.widget.TextView;
  10.  
  11. /**
  12. * Description: <br/>
  13. * site: <a href="http://www.crazyit.org">crazyit.org</a> <br/>
  14. * Copyright (C), 2001-2014, Yeeku.H.Lee <br/>
  15. * This program is protected by copyright laws. <br/>
  16. * Program Name: <br/>
  17. * Date:
  18. *
  19. * @author Yeeku.H.Lee kongyeeku@163.com
  20. * @version 1.0
  21. */
  22. public class ContextMenuTest extends Activity {
  23. // 为每个菜单定义一个标识
  24. final int MENU1 = 0x111;
  25. final int MENU2 = 0x112;
  26. final int MENU3 = 0x113;
  27. private TextView txt;
  28. int selectedIndex = -1;
  29.  
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.main);
  34. txt = (TextView) findViewById(R.id.txt);
  35. // 为文本框注册上下文菜单
  36. registerForContextMenu(txt); // ①
  37. }
  38.  
  39. // 创建上下文菜单时触发该方法
  40. @Override
  41. public void onCreateContextMenu(ContextMenu menu, View source,
  42. ContextMenu.ContextMenuInfo menuInfo) {
  43. menu.add(0, MENU1, 0, "红色");
  44. menu.add(0, MENU2, 0, "绿色");
  45. menu.add(0, MENU3, 0, "蓝色");
  46. // 将三个菜单项设为单选菜单项
  47. menu.setGroupCheckable(0, true, true);
  48. // 设置上下文菜单的标题、图标
  49. menu.setHeaderIcon(R.drawable.tools);
  50. menu.setHeaderTitle("选择背景色");
  51. if (selectedIndex >= 0) {
  52. menu.getItem(selectedIndex).setChecked(true);
  53. }
  54. }
  55.  
  56. // 上下菜单的菜单项被单击时触发该方法。
  57. @Override
  58. public boolean onContextItemSelected(MenuItem mi) {
  59. switch (mi.getItemId()) {
  60. case MENU1:
  61. txt.setBackgroundColor(Color.RED);
  62. selectedIndex = 0;
  63. break;
  64. case MENU2:
  65. txt.setBackgroundColor(Color.GREEN);
  66. selectedIndex = 1;
  67. break;
  68. case MENU3:
  69. txt.setBackgroundColor(Color.BLUE);
  70. selectedIndex = 2;
  71. break;
  72. }
  73. return true;
  74. }
  75. }

截图:

CONTEXT MENU简介的更多相关文章

  1. Win7/Win8右键菜单管理工具(Easy Context Menu) v1.5 绿色版

    软件名称: Win7/Win8右键菜单管理工具(Easy Context Menu)软件语言: 简体中文授权方式: 免费软件运行环境: Win8 / Win7 / Vista / WinXP软件大小: ...

  2. Android Contextual Menus之一:floating context menu

    Android Contextual Menus之一:floating context menu 上下文菜单 上下文相关的菜单(contextual menu)用来提供影响UI中特定item或者con ...

  3. Tree Context Menu

    Right click on a node to display context menu.   My Documents Photos Program Files Intel Java Micros ...

  4. create Context Menu in Windows Forms application using C# z

    In this article let us see how to create Context Menu in Windows Forms application using C# Introduc ...

  5. Description Resource Path Location Type Project configuration is not up-to-date with pom.xml. Select: Maven->Update Project... from the project context menu or use Quick Fix. spark-MT line 1 Maven Co

    1.相信大家新建的maven项目,然后添加好依赖(即修改了pom.xml文件以后就会出现如下所示的错误): Description Resource Path Location Type Projec ...

  6. sublime text 3-right click context menu

    dd a system wide windows explorer button " Edit with Sublime" similar to how Notepad++ doe ...

  7. 手机浏览器中屏蔽img的系统右键菜单context menu

    我们知道通过oncontextmenu事件可以屏蔽浏览器右键菜单 $('img').on("contextmenu",function(E){E.preventDefault(); ...

  8. Android -- Options Menu,Context Menu,Popup Menu

    Options Menu                                                                           创建选项菜单的步骤: 1. ...

  9. Vue 2.0 右键菜单组件 Vue Context Menu

    Vue 2.0 右键菜单组件 Vue Context Menu https://juejin.im/entry/5976d14751882507db6e839c

随机推荐

  1. Linux网络编程echo多线程服务器

    echo_server服务器多线程版本 #include <unistd.h> #include <stdlib.h> #include <stdio.h> #in ...

  2. SVN server的搭建

    当做大的项目是,svn是代码管理的好工具,假设是用自己的server,那么须要搭建SVNserver. Subversion是一款很优秀的svnserver工具,笔者採用VisualSVN serve ...

  3. uva:10340 - All in All(字符串匹配)

    题目:10340 - All in All 题目大意:给出字符串s和t,问s是否是t的子串.s若去掉某些字符能和t一样,那么t是s的子串. 解题思路:匹配字符.t的每一个字符和s中的字符匹配.注意这里 ...

  4. Lucene中string docvalues使用utf-16的优化

    原来的string docvalues使用utf-8编码,载入时转码花费大量时间,我们把转码实现从new String(bytes, "UTF-8")改用lucene的bytesR ...

  5. python 学习笔记 基础

    python对象三要素: identity(值):对应于内存的地址,不可修改type(类型):不可修改value(值): mutable :可以修改 immutable:不可以修改 引用计数当引用计数 ...

  6. rsyslog安装

    http://www.rsyslog.com/downloads/download-other/ http://www.rsyslog.com/download/stable-download/pag ...

  7. is not in the sudoers file.This incident will be reported

    解决方法如下: 1>.进入超级用户模式.也就是输入"su -",系统会让你输入超级用户密码,输入密码后就进入了超级用户模式. 2>.添加文件的写权限.也就是输入命令&q ...

  8. WAMP 环境下,YII创建失败 提示 "'php.exe' 不是内部或外部命..."

    现象: http://www.yiichina.com/guide/quickstart.first-app 使用这里的命令  % YiiRoot/framework/yiic webapp WebR ...

  9. POJ 1470 Closest Common Ancestors(LCA&RMQ)

    题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...

  10. Java 图片与byte数组互相转换

    //图片到byte数组 public byte[] image2byte(String path){ byte[] data = null; FileImageInputStream input = ...