布局文件main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--标题栏-->
<ImageView
android:id="@+id/img_title"
android:layout_width="match_parent"
android:layout_height="@dimen/title_height"
android:scaleType="fitXY"
android:src="@drawable/wei_top" />
<!--头像-->
<ImageView
android:id="@+id/img_head"
android:layout_width="@dimen/head"
android:layout_height="@dimen/head"
android:layout_below="@+id/img_title"
android:src="@mipmap/tx3"/>
<!--用户名-->
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/img_head"
android:text="CSDN梦梦"
android:textSize="@dimen/userNameSize"
android:layout_marginLeft="@dimen/margin_headLeft"
android:layout_below="@+id/img_title"
android:textColor="@color/colorUsername"/>
<!--内容-->
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
android:layout_below="@+id/username"
android:layout_toRightOf="@+id/img_head"
android:textColor="@color/colorContent"
android:textSize="@dimen/content_textSize"
android:layout_marginLeft="@dimen/margin_headLeft"/> <TextView
android:id="@+id/time_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="12小时前"
android:layout_toRightOf="@+id/img_head"
android:layout_marginLeft="@dimen/margin_headLeft"
android:layout_marginTop="10dp"
android:textColor="@android:color/darker_gray"
android:layout_below="@id/content"/>
</RelativeLayout>

菜单文件menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_copy" android:title="复制"></item>
<item android:id="@+id/menu_collect" android:title="收藏"></item>
<item android:id="@+id/menu_translate" android:title="翻译"></item>
<item android:id="@+id/menu_report" android:title="举报"></item>
</menu>

MainActivity.java

package com.lzp.circleoffriend;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { TextView content; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//为文本框注册上下文菜单
TextView content = findViewById(R.id.content);
registerForContextMenu(content);
} //重写onCreateContextMunu()方法,添加一个上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.menu,menu);
}
//重写onContextItemSelected,对每个菜单的选中事件做处理
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.menu_copy:
Toast.makeText(MainActivity.this,
"已复制",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_collect:
Toast.makeText(MainActivity.this,
"已收藏",Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
}

效果:

Android学习笔记上下文菜单的更多相关文章

  1. android学习笔记23——菜单

    菜单在桌面应用程序中使用非常广泛,由于手机屏幕的制约,菜单在手机应用中减少不少. android应用中的菜单默认是不可见的,只有当用户单击手机上“Menu”键时,系统才会显示该应用关联的采用项. an ...

  2. Android学习笔记--Menu菜单的使用

    实现选项菜单.上下文菜单,以及菜单内部的子菜单. 视图效果: MainActivity 选项菜单 选项菜单的子菜单 上下文菜单(按住按钮或者EditText弹出) 注意:上下文菜单如何弹出?在注册该菜 ...

  3. 【转】 Pro Android学习笔记(三五):Menu(6):XML方式 & PopUp菜单

    目录(?)[-] 利用XML创建菜单 XML的有关属性 onClick事件 Pop-up菜单 利用XML创建菜单 在代码中对每个菜单项进行设置,繁琐且修改不灵活,不能适配多国语言的要求,可以利用资源进 ...

  4. 【转】 Pro Android学习笔记(三三):Menu(4):Alternative菜单

    目录(?)[-] 什么是Alternative menu替代菜单 小例子说明 Alternative menu代码 关于Category和规范代码写法 关于flags 多个匹配的itemId等参数 什 ...

  5. Android学习笔记——Menu(二)

    知识点: 这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu). 上下文菜单 上下文菜单提供对UI界面上的特定项或上下文框架的操作,就 ...

  6. Pro Android学习笔记 ActionBar(1):Home图标区

     Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...

  7. 【转】 Pro Android学习笔记(五六):配置变化

    目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...

  8. 【转】 Pro Android学习笔记(五二):ActionBar(5):list模式

    可以在action bar中加入spinner的下来菜单,有关spinner,可以参考Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner. list的样式和 ...

  9. 【转】Pro Android学习笔记(三十):Menu(1):了解Menu

    目录(?)[-] 创建Menu MenuItem的属性itemId MenuItem的属性groupId MenuItem的属性orderId MenuItem的属性可选属性 Menu触发 onOpt ...

随机推荐

  1. 你以为只有马云会灌鸡汤?Linux 命令行也会!

    你以为只有马云会灌鸡汤?Linux 命令行也会! "Linux 太南了o(╥﹏╥)o","我累了不想奋斗了o(︶︿︶)o"... 不知道你有没有想过,在你快丧失 ...

  2. JS获取两个日期间的所有日期

    var stime = '2018-07-25'; //开始日期 var etime = '2018-08-02'; //结束日期 getdiffdate(stime,etime); //获取两日期之 ...

  3. 害你加班的bug就是我写的,记一次升级Jenkins插件引发的加班

    主旨 本文主要记录了下Jenkins升级插件过程中出现的场景,一次加班经历,事发时没有截图,有兴趣可以看看. 起因 需求 最近有个需求:在Jenkins流水线中完成下载Git上的文件简单修改并提交的功 ...

  4. 微软:正式发布针对 .NET Core的 Winform 设计器

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://devblogs.microsoft.com/dotnet/windows-for ...

  5. ngnix随笔一

    nginx安装及启动 1.用yum源安装,首先配置yum源 在“/etc/yum.repo.d/”下创建yum源文件 例如:cd /etc/yum.repo.d/nginx.repo [nginx-s ...

  6. KEA128+SHT30+CRC校验

    最近更新产品功能的时候使用到Sensirion的SHT30(温湿度传感器),虽说官网上有例程(STM32F100RB),但用的是软件模拟I2C时序控制SHT30进行温湿度读取,我用的是S9KEA128 ...

  7. 【Ubuntu】安装Ubuntu18.04.2LTS

    环境:win10专业版.联想30D9主板 ubuntu:18.04.2LTS:Ubuntu镜像传送门:https://ubuntu.com/download/desktop 有两块硬盘,win10安装 ...

  8. 正则表达式:匹配单个数字重复n次

    匹配单个数字重复n次:(\d)\1{n-1}其中,\d表示一位数字,(\d)表示匹配之后捕获该匹配,并分组并对组进行编号\1表示被捕获的第一个分组{n-1}是因为被捕获的第一个分组已经消耗了一位数字, ...

  9. 图解MySQL索引(二)—为什么使用B+Tree

    失踪人口回归,近期换工作一波三折,耽误了不少时间,从今开始每周更新~ 索引是一种支持快速查询的数据结构,同时索引优化也是后端工程师的必会知识点.各个公司都有所谓的MySQL"军规" ...

  10. Java rmi漏洞利用及原理记录

    CVE-2011-3556 该模块利用了RMI的默认配置.注册表和RMI激活服务,允许加载类来自任何远程(HTTP)URL.当它在RMI中调用一个方法时分布式垃圾收集器,可通过每个RMI使用endpo ...