布局文件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. Ajax长轮询(LongPoll)

    1)HTML HTTP是无状态.单向的协议,用户只能够通过客服端向服务器发送请求并由服务器处理发回一个响应.若要实现聊天室.WEBQQ.在线客服.邮箱等这些即时通讯的应用,就要用到“ 服务器推送技术( ...

  2. h5 js判断是IOS系统还是android系统

    function detect(){ var equipmentType = ""; var agent = navigator.userAgent.toLowerCase(); ...

  3. 解决python引包错误

    # coding=utf8# date = 2019/12/23 19:54# 清白丶之年__照林""" # Solve Import Lib Error Add Som ...

  4. MySQL8.0 忘记密码、重置密码

    修改my.cnf [mysqld] 域中添加skip-grant-tables 重启mysqld服务 systemctl restart mysqld 重新使用空密码登录,直接敲回车 mysql -u ...

  5. 脚本:Tomcat日志切割

    Tomcat日志切割脚本 #!/bin/bash #Tomcat日志切割 Tomcat_logs_path=/data/server/tomcat-8080/logs d=`date +%F` d7= ...

  6. Chisel3 - model - Hardware Model

    https://mp.weixin.qq.com/s/x6j7LZg7i7i_KcNEA8YCQw   Chisel作为领域专用语言(DSL),用于构建硬件模型.待硬件模型建立后,再基于模型进行仿真. ...

  7. Java实现 蓝桥杯 算法提高 小X的购物计划

    试题 算法提高 小X的购物计划 问题描述 小X打算去超市shopping.小X没什么钱,只有N元.超市里有M种物品,每种物品都需要money,在小X心中有一个重要度.有的物品有无限件,有的物品只有几件 ...

  8. Java实现 LeetCode 502 IPO(LeetCode:我疯起来连自己都卖)

    502. IPO 假设 力扣(LeetCode)即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完 ...

  9. Java实现 蓝桥杯VIP 算法训练 ALGO-85进制转换

    问题描述 编写一个程序,输入一个二进制的字符串(长度不超过32),然后计算出相应的十进制整数,并把它打印出来. 输入格式:输入为一个字符串,每个字符都是'0'或'1',字符串的长度不超过32. 输出格 ...

  10. Java实现 蓝桥杯VIP 算法提高 计算时间

    算法提高 计算时间 时间限制:1.0s 内存限制:512.0MB 问题描述 给定一个t,将t秒转化为HH:MM:SS的形式,表示HH小时MM分钟SS秒.HH,MM,SS均是两位数,如果小于10用0补到 ...