ContextMenu,称为上下文菜单,也就是长按界面不放,弹出的菜单。使用ContextMenu有三个步骤:

(1)调用registerForContextMenu()方法,为视图注册上下文菜单;

(2)重写Activity的onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo)方法,调用Menu的add方法添加菜单项(MenuItem)

(3)重写onContextItemSelected(MenuItem item)方法,响应菜单单击事件;

注:步骤(2)中的方法参数ContextMenuInfo menuInfo有什么作用?

有时候,视图元素需要向上下文菜单传递一些信息,比如该View对应DB记录的ID等,这就要使用ContextMenuInfo。需要传递额外信息的View需要重写getContextMenuInfo()方法,返回一个带有数据的ContextMenuInfo实现类对象。

以下程序参考自《android应用开发详解》

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private TextView contextMenuView; //定义TextView
private static final int ITEM1=Menu.FIRST;
private static final int ITEM2=Menu.FIRST+1;
private static final int ITEM3=Menu.FIRST+2;
private static final int ITEM4=Menu.FIRST+3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contextMenuView=(TextView)findViewById(R.id.contextTest);
//注册上下文菜单
registerForContextMenu(contextMenuView); } //重写方法,并添加菜单选项
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){
menu.add(0,ITEM1,0,"复制");
menu.add(0,ITEM2,0,"全选");
menu.add(0,ITEM3,0,"分享");
menu.add(0,ITEM4,0,"搜索");
}

//响应菜单事件
public boolean onContextItemSelected(MenuItem item){
switch(item.getItemId()){
case ITEM1:
Toast.makeText(this, "You have click 'copy'",Toast.LENGTH_SHORT ).show();
break;
case ITEM2:
Toast.makeText(this, "You have click 'all copy'",Toast.LENGTH_SHORT ).show();
break;
case ITEM3:
Toast.makeText(this, "You have click 'share'",Toast.LENGTH_SHORT ).show();
break;
case ITEM4:
Toast.makeText(this, "You have click 'search'",Toast.LENGTH_SHORT ).show();
break; }
return true;
}
}

activity_main.xml如下 :

 <TextView
android:id="@+id/contextTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长按屏幕显示ContextMenu" />

代码运行结果如下:

Android笔记:ContextMenu的更多相关文章

  1. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  2. Android之ContextMenu的使用方法以及与OptionMenu的区别

    >> ContextMenu是android的context menu上下文菜单,选择某项VIEW后长按menu键,就会显示出来.比如EditeText就可以通过长按来弹出拥有“cut”, ...

  3. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

  4. Android 笔记之 R 文件

    Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...

  5. Android 笔记之 Android 系统架构

    Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...

  6. Android笔记之使用Glide加载网络图片、下载图片

    Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...

  7. Android笔记--View绘制流程源码分析(二)

    Android笔记--View绘制流程源码分析二 通过上一篇View绘制流程源码分析一可以知晓整个绘制流程之前,在activity启动过程中: Window的建立(activit.attach生成), ...

  8. Android笔记--View绘制流程源码分析(一)

    Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...

  9. Android笔记--自定义控件仿遥控器的圆形上下左右OK圆盘按钮

    原文:Android笔记--自定义控件仿遥控器的圆形上下左右OK圆盘按钮 上面就是几张预览图!代码在最底下 主要就两个步骤,画图.监听点击 1.整个控件基本上是一步步画出来的,重写onDraw方法开始 ...

随机推荐

  1. 学python的第二天

    我是一个有一点点c语言基础的大二学生,今天的积累 cd指令=change directory(目录) dir=查看当前目录文件列表 convert   转化   defind  定义 cd ..   ...

  2. 坑爹的myeclipse 的tomcat 重部署 redeploy !

    启动 tomcat 出现: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bea ...

  3. 1. ip正则表达式验证

    java: private boolean IsIpAddressValidOrEmpty(String ip) { if (ip == null) return false; String temp ...

  4. JMeter之Ramp-up Period(in seconds)说明

    Ramp-up Period(in seconds) [1]决定多长时间启动所有线程.如果使用10个线程,ramp-up period是100秒,那么JMeter用100秒使所有10个线程启动并运行. ...

  5. Flex 画图

    <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="h ...

  6. ubuntu 上 SSH scp 技巧

    参考:https://deepzz.com/post/how-to-setup-ssh-config.html SSH(Secure Shell)是什么?是一项创建在应用层和传输层基础上的安全协议,为 ...

  7. kong API gateway

    参考:https://www.cnblogs.com/chenjinxi/p/8724564.html 一.简介 Kong,是由Mashape公司开源的,基于Nginx的API gateway. 二. ...

  8. idea 添加代码自动提示支持,已PHP扩展 swoole 为例

    1,下载代码支持包 => swoole-ide-helper-en => https://github.com/eaglewu/swoole-ide-helper.git 2,如果安装了 ...

  9. githup创建新java项目

    1.在githup中创建仓库 2.import创建的地址到本地文件d:/mygit 3.在d:/mygit中创建eclipse项目 3.在eclipse中team-->push to branc ...

  10. leetcode1020

    class Solution(object): def __init__(self): self.cons = 0 self.S = list() def dfs(self,m,n,v,A): whi ...