布局文件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. Security8:权限模拟

    用户可以模拟其他用户或登陆的权限来执行查询,并且在查看用户和登录的权限时,结果会受到模拟上下文的影响.当执行EXECUTE AS命令时,原始用户的安全上下文会进行切换,除了ORIGINAL_LOGIN ...

  2. mysql 的大文本存储TEXT & BLOB

    TEXT & BLOB 一般在保存少量字符串的时候,我们会选择 CHAR 或者 VARCHAR:而在保存较大文本时,通常会选择使用 TEXT 或者 BLOB,二者之间的主要差别是 BLOB 能 ...

  3. 4.Linux的目录结构

    Linux的目录结构 (1)"/"目录 Linux文件系统的入口,也是出于最高一级的目录 (2)"/bin" 基础系统所需要的那些命令位于此目录.也是最小系统所 ...

  4. MySQL 5.7.30 的安装/升级(所有可能的坑都在这里)

    楔子 由于之前电脑上安装的MySQL版本是比较老的了,大概是5.1的版本,不支持JSON字段功能.而最新开发部门开发的的编辑器产品,使用到了JSON字段的功能. 因此需要升级MySQL版本,升级的目标 ...

  5. Spring_AOP_AspectJ支持的通知注解

    1.AOP前奏: 使用动态代理解决日志需求 ArithmeticCalculator.java package com.aff.spring.aop.helloworld; public interf ...

  6. Rocket - debug - SBA

    https://mp.weixin.qq.com/s/eFOHrEhvq2PlEJ14j2vlhg 简单介绍SBA的实现. 1. SystemBusAccessState 系统总线访问状态: 分别是: ...

  7. Rocket - interrupts - Crossing

    https://mp.weixin.qq.com/s/nSX4prXFb4K5GSUhPtOUCg 简单介绍Crossing的实现. 1. IntXing 这是一个LazyModule: 1) 参数 ...

  8. 和付费网盘说再见,跟着本文自己起个网盘(Java 开源项目)

    本文适合有 Java 基础知识的人群,跟着本文可学习和运行 Java 网盘项目. 本文作者:HelloGitHub-秦人 HelloGitHub 推出的<讲解开源项目>系列. 今天给大家带 ...

  9. Java实现 蓝桥杯VIP 基础练习 2n皇后问题

    基础练习 2n皇后问题 时间限制:1.0s 内存限制:512.0MB 问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一 ...

  10. Java实现 蓝桥杯VIP 算法提高 打水问题

    算法提高 打水问题 时间限制:1.0s 内存限制:512.0MB 问题描述 N个人要打水,有M个水龙头,第i个人打水所需时间为Ti,请安排一个合理的方案使得所有人的等待时间之和尽量小. 输入格式 第一 ...