CONTEXT MENU简介
安卓中的上下文菜单是通过长按控件元素触发的,要注意的是每次都会触发onCreateContextMenu方法:
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/txt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="可通过上下文菜单修改背景色"
- android:textSize="15pt" />
- </LinearLayout>
ContextMenuTest.java
- package org.crazyit.ui;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.ContextMenu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.TextView;
- /**
- * Description: <br/>
- * site: <a href="http://www.crazyit.org">crazyit.org</a> <br/>
- * Copyright (C), 2001-2014, Yeeku.H.Lee <br/>
- * This program is protected by copyright laws. <br/>
- * Program Name: <br/>
- * Date:
- *
- * @author Yeeku.H.Lee kongyeeku@163.com
- * @version 1.0
- */
- public class ContextMenuTest extends Activity {
- // 为每个菜单定义一个标识
- final int MENU1 = 0x111;
- final int MENU2 = 0x112;
- final int MENU3 = 0x113;
- private TextView txt;
- int selectedIndex = -1;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- txt = (TextView) findViewById(R.id.txt);
- // 为文本框注册上下文菜单
- registerForContextMenu(txt); // ①
- }
- // 创建上下文菜单时触发该方法
- @Override
- public void onCreateContextMenu(ContextMenu menu, View source,
- ContextMenu.ContextMenuInfo menuInfo) {
- menu.add(0, MENU1, 0, "红色");
- menu.add(0, MENU2, 0, "绿色");
- menu.add(0, MENU3, 0, "蓝色");
- // 将三个菜单项设为单选菜单项
- menu.setGroupCheckable(0, true, true);
- // 设置上下文菜单的标题、图标
- menu.setHeaderIcon(R.drawable.tools);
- menu.setHeaderTitle("选择背景色");
- if (selectedIndex >= 0) {
- menu.getItem(selectedIndex).setChecked(true);
- }
- }
- // 上下菜单的菜单项被单击时触发该方法。
- @Override
- public boolean onContextItemSelected(MenuItem mi) {
- switch (mi.getItemId()) {
- case MENU1:
- txt.setBackgroundColor(Color.RED);
- selectedIndex = 0;
- break;
- case MENU2:
- txt.setBackgroundColor(Color.GREEN);
- selectedIndex = 1;
- break;
- case MENU3:
- txt.setBackgroundColor(Color.BLUE);
- selectedIndex = 2;
- break;
- }
- return true;
- }
- }
截图:
CONTEXT MENU简介的更多相关文章
- Win7/Win8右键菜单管理工具(Easy Context Menu) v1.5 绿色版
软件名称: Win7/Win8右键菜单管理工具(Easy Context Menu)软件语言: 简体中文授权方式: 免费软件运行环境: Win8 / Win7 / Vista / WinXP软件大小: ...
- Android Contextual Menus之一:floating context menu
Android Contextual Menus之一:floating context menu 上下文菜单 上下文相关的菜单(contextual menu)用来提供影响UI中特定item或者con ...
- Tree Context Menu
Right click on a node to display context menu. My Documents Photos Program Files Intel Java Micros ...
- 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 ...
- 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 ...
- sublime text 3-right click context menu
dd a system wide windows explorer button " Edit with Sublime" similar to how Notepad++ doe ...
- 手机浏览器中屏蔽img的系统右键菜单context menu
我们知道通过oncontextmenu事件可以屏蔽浏览器右键菜单 $('img').on("contextmenu",function(E){E.preventDefault(); ...
- Android -- Options Menu,Context Menu,Popup Menu
Options Menu 创建选项菜单的步骤: 1. ...
- Vue 2.0 右键菜单组件 Vue Context Menu
Vue 2.0 右键菜单组件 Vue Context Menu https://juejin.im/entry/5976d14751882507db6e839c
随机推荐
- Linux网络编程echo多线程服务器
echo_server服务器多线程版本 #include <unistd.h> #include <stdlib.h> #include <stdio.h> #in ...
- SVN server的搭建
当做大的项目是,svn是代码管理的好工具,假设是用自己的server,那么须要搭建SVNserver. Subversion是一款很优秀的svnserver工具,笔者採用VisualSVN serve ...
- uva:10340 - All in All(字符串匹配)
题目:10340 - All in All 题目大意:给出字符串s和t,问s是否是t的子串.s若去掉某些字符能和t一样,那么t是s的子串. 解题思路:匹配字符.t的每一个字符和s中的字符匹配.注意这里 ...
- Lucene中string docvalues使用utf-16的优化
原来的string docvalues使用utf-8编码,载入时转码花费大量时间,我们把转码实现从new String(bytes, "UTF-8")改用lucene的bytesR ...
- python 学习笔记 基础
python对象三要素: identity(值):对应于内存的地址,不可修改type(类型):不可修改value(值): mutable :可以修改 immutable:不可以修改 引用计数当引用计数 ...
- rsyslog安装
http://www.rsyslog.com/downloads/download-other/ http://www.rsyslog.com/download/stable-download/pag ...
- is not in the sudoers file.This incident will be reported
解决方法如下: 1>.进入超级用户模式.也就是输入"su -",系统会让你输入超级用户密码,输入密码后就进入了超级用户模式. 2>.添加文件的写权限.也就是输入命令&q ...
- WAMP 环境下,YII创建失败 提示 "'php.exe' 不是内部或外部命..."
现象: http://www.yiichina.com/guide/quickstart.first-app 使用这里的命令 % YiiRoot/framework/yiic webapp WebR ...
- POJ 1470 Closest Common Ancestors(LCA&RMQ)
题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...
- Java 图片与byte数组互相转换
//图片到byte数组 public byte[] image2byte(String path){ byte[] data = null; FileImageInputStream input = ...