Android学习笔记上下文菜单
布局文件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学习笔记上下文菜单的更多相关文章
- android学习笔记23——菜单
菜单在桌面应用程序中使用非常广泛,由于手机屏幕的制约,菜单在手机应用中减少不少. android应用中的菜单默认是不可见的,只有当用户单击手机上“Menu”键时,系统才会显示该应用关联的采用项. an ...
- Android学习笔记--Menu菜单的使用
实现选项菜单.上下文菜单,以及菜单内部的子菜单. 视图效果: MainActivity 选项菜单 选项菜单的子菜单 上下文菜单(按住按钮或者EditText弹出) 注意:上下文菜单如何弹出?在注册该菜 ...
- 【转】 Pro Android学习笔记(三五):Menu(6):XML方式 & PopUp菜单
目录(?)[-] 利用XML创建菜单 XML的有关属性 onClick事件 Pop-up菜单 利用XML创建菜单 在代码中对每个菜单项进行设置,繁琐且修改不灵活,不能适配多国语言的要求,可以利用资源进 ...
- 【转】 Pro Android学习笔记(三三):Menu(4):Alternative菜单
目录(?)[-] 什么是Alternative menu替代菜单 小例子说明 Alternative menu代码 关于Category和规范代码写法 关于flags 多个匹配的itemId等参数 什 ...
- Android学习笔记——Menu(二)
知识点: 这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu). 上下文菜单 上下文菜单提供对UI界面上的特定项或上下文框架的操作,就 ...
- Pro Android学习笔记 ActionBar(1):Home图标区
Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...
- 【转】 Pro Android学习笔记(五六):配置变化
目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...
- 【转】 Pro Android学习笔记(五二):ActionBar(5):list模式
可以在action bar中加入spinner的下来菜单,有关spinner,可以参考Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner. list的样式和 ...
- 【转】Pro Android学习笔记(三十):Menu(1):了解Menu
目录(?)[-] 创建Menu MenuItem的属性itemId MenuItem的属性groupId MenuItem的属性orderId MenuItem的属性可选属性 Menu触发 onOpt ...
随机推荐
- 【百度网盘活动】扩容2T+7天会员
https://pan.baidu.com/component/view?id=455 https://pan.baidu.com/union/smartProgramShare?scheme=bdn ...
- 【python爬虫】scrapy入门6-生成多个spider
一个工程生产一个spider,也可以多个spider,比如一个爬文本,一个爬图片等 cd tutorial #自己创建的工程目录 scrapy genspider test1 test1.com sc ...
- Java——java.lang.NumberFormatException: For input string: ""
java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.f ...
- java——定时任务
java定时任务直接看代码 public void timeTask(){ Timer timer = new Timer(); timer.schedule(new TimerTask() { pu ...
- web自动化之鼠标事件
鼠标操作from selenium.webdriver.common.action_chains import ActionChains通过ActionChains 类来完成鼠标操作主要操作流程:1. ...
- C#线程 使用线程
第三部分 使用线程 基于事件的异步模式 基于事件的异步模式(EAP)提供了一种简单的方法,通过这些方法,类可以提供多线程功能,而使用者无需显式启动或管理线程.它还提供以下功能: 合作取消模型 工作人员 ...
- Python装饰器的一点解读
版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/12977127.html 作者:窗户 ...
- VC程序设计--文字输出方法与字体示例
在用户窗口上输出一个扇形,并在扇面竖向输出一首唐诗.本例使用绝对定位确定输出文字的位置,并采用多种自定义字体输出文字. // poemDemo.cpp : 定义应用程序的入口点. // #includ ...
- ucoreos_lab1
前言 最近觉得自己之前蛮多基础课学的并不咋滴,便想再补补.前段时间突然看到清华的操作系统实验,于是乎就打算试试,一边学一边做实验,然后通过博客来记录记录. 实验内容 lab1 中包含一个 bootlo ...
- Java实现 LeetCode 538 把二叉搜索树转换为累加树(遍历树)
538. 把二叉搜索树转换为累加树 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和 ...