android开发步步为营之27:上下文菜单ContextMenu的用法
来源:互联网 发布:淘宝怎么看追加评论 编辑:程序博客网 时间:2024/05/21 11:23
ContextMenu上下文菜单,它是通过长时间的按住一个view弹出来的菜单,比如长时间按住一个图片、文本等,都可以触发弹出一个上下文菜单。可以根据选择的菜单,弹出相应的消息或者跳转到相关的Activity中去。
public interface ContextMenu implements Menu
android.view.ContextMenu
Class Overview
Extension of Menu for context menus providing functionality to modify the header of the context menu.
Context menus do not support item shortcuts and item icons.
To show a context menu on long click, most clients will want to call registerForContextMenu(View) and override onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo).
开始我们的实验。
第一步、设计页面contextmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/imgHead" android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></ImageView>
<TextView android:layout_width="wrap_content" android:id="@+id/tvName" android:layout_height="wrap_content" android:text="姓名" android:layout_alignBottom="@+id/imgHead" android:layout_toRightOf="@+id/imgHead" android:layout_marginLeft="34dp" android:layout_marginBottom="15dp"></TextView>
</RelativeLayout>
第二步、设计Activity ContextMenuActivity.java
/**
*
*/
package com.figo.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author zhuzhifei
*
*/
public class ContextMenuActivity extends Activity {
TextView tvName;
ImageView imgHead;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contextmenu);
tvName = (TextView) findViewById(R.id.tvName);
imgHead = (ImageView) findViewById(R.id.imgHead);
// 给两个view注册上下文菜单
this.registerForContextMenu(tvName);
this.registerForContextMenu(imgHead);
}
// 创建上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
switch (v.getId()) {
case R.id.tvName:
menu.setHeaderIcon(R.drawable.icon);
menu.setHeaderTitle("测试上下文菜单");
//menu.clearHeader();
// 第一个参数:组ID
// 第二个参数:菜单项ID
// 第三个参数:顺序号
// 第四个参数:菜单项上显示的内容
menu.add(1,0,0,"菜单1");
menu.add(1,1,1,"菜单2").setCheckable(true); // 增加一个√选项
break;
case R.id.imgHead:
// ContextMenu.addSubMenu("菜单名称") - 用来添加子菜单。子菜单其实就是一个特殊的菜单
SubMenu sub1 = menu.addSubMenu("父菜单1");
sub1.setHeaderIcon(R.drawable.icon);
sub1.add(0, 0, 0, "菜单1");
sub1.add(0, 1, 1, "菜单2");
sub1.setGroupCheckable(1, true, true);
SubMenu sub2 = menu.addSubMenu("父菜单2");
sub2.setIcon(R.drawable.icon);
sub2.add(1, 0, 0, "菜单3");
sub2.add(1, 1, 1, "菜单4");
sub2.setGroupCheckable(1, true, true);
break;
}
}
@Override
public void openContextMenu(View view) {
// TODO Auto-generated method stub
super.openContextMenu(view);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
// return super.onContextItemSelected(item);
Toast.makeText(getApplicationContext(),"您选的菜单GroupId:"+item.getGroupId()+",ItemId:"+item.getItemId()+",Order:"+item.getOrder()+",Title:"+ item.getTitle(), Toast.LENGTH_LONG).show();
return true;
}
@Override
public void onContextMenuClosed(Menu menu) {
// TODO Auto-generated method stub
super.onContextMenuClosed(menu);
}
}
第三步、AndroidManifest.xml 注册Activity
<activity android:name=".ContextMenuActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
第四步、运行效果
public interface ContextMenu implements Menu
android.view.ContextMenu
Class Overview
Extension of Menu for context menus providing functionality to modify the header of the context menu.
Context menus do not support item shortcuts and item icons.
To show a context menu on long click, most clients will want to call registerForContextMenu(View) and override onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo).
开始我们的实验。
第一步、设计页面contextmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/imgHead" android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></ImageView>
<TextView android:layout_width="wrap_content" android:id="@+id/tvName" android:layout_height="wrap_content" android:text="姓名" android:layout_alignBottom="@+id/imgHead" android:layout_toRightOf="@+id/imgHead" android:layout_marginLeft="34dp" android:layout_marginBottom="15dp"></TextView>
</RelativeLayout>
第二步、设计Activity ContextMenuActivity.java
/**
*
*/
package com.figo.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author zhuzhifei
*
*/
public class ContextMenuActivity extends Activity {
TextView tvName;
ImageView imgHead;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contextmenu);
tvName = (TextView) findViewById(R.id.tvName);
imgHead = (ImageView) findViewById(R.id.imgHead);
// 给两个view注册上下文菜单
this.registerForContextMenu(tvName);
this.registerForContextMenu(imgHead);
}
// 创建上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
switch (v.getId()) {
case R.id.tvName:
menu.setHeaderIcon(R.drawable.icon);
menu.setHeaderTitle("测试上下文菜单");
//menu.clearHeader();
// 第一个参数:组ID
// 第二个参数:菜单项ID
// 第三个参数:顺序号
// 第四个参数:菜单项上显示的内容
menu.add(1,0,0,"菜单1");
menu.add(1,1,1,"菜单2").setCheckable(true); // 增加一个√选项
break;
case R.id.imgHead:
// ContextMenu.addSubMenu("菜单名称") - 用来添加子菜单。子菜单其实就是一个特殊的菜单
SubMenu sub1 = menu.addSubMenu("父菜单1");
sub1.setHeaderIcon(R.drawable.icon);
sub1.add(0, 0, 0, "菜单1");
sub1.add(0, 1, 1, "菜单2");
sub1.setGroupCheckable(1, true, true);
SubMenu sub2 = menu.addSubMenu("父菜单2");
sub2.setIcon(R.drawable.icon);
sub2.add(1, 0, 0, "菜单3");
sub2.add(1, 1, 1, "菜单4");
sub2.setGroupCheckable(1, true, true);
break;
}
}
@Override
public void openContextMenu(View view) {
// TODO Auto-generated method stub
super.openContextMenu(view);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
// return super.onContextItemSelected(item);
Toast.makeText(getApplicationContext(),"您选的菜单GroupId:"+item.getGroupId()+",ItemId:"+item.getItemId()+",Order:"+item.getOrder()+",Title:"+ item.getTitle(), Toast.LENGTH_LONG).show();
return true;
}
@Override
public void onContextMenuClosed(Menu menu) {
// TODO Auto-generated method stub
super.onContextMenuClosed(menu);
}
}
第三步、AndroidManifest.xml 注册Activity
<activity android:name=".ContextMenuActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
第四步、运行效果
0 0
- android开发步步为营之27:上下文菜单ContextMenu的用法
- Android开发之创建ContextMenu上下文菜单
- Android 之ContextMenu上下文菜单
- android开发步步为营之29:选项菜单OptionsMenu的用法
- Android--ContextMenu上下文菜单
- 【Android】上下文菜单contextMenu
- Android上下文菜单 ContextMenu
- Android-ContextMenu上下文菜单
- 【Android】上下文菜单contextMenu
- Android ContextMenu 上下文菜单
- Android开发之Menu:OptionMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)
- 【Android】上下文菜单ContextMenu的使用
- 【Android】上下文菜单ContextMenu的使用
- Android菜单使用之ContextMenu(上下文菜单)
- Android——菜单之ContextMenu(上下文菜单)
- 12. android ContextMenu 上下文菜单
- android 使用上下文菜单ContextMenu
- android ContextMenu 上下文菜单示例
- 2014校园招聘之二(3月阿里巴巴实习生笔试题)
- c语言面试知识总结
- 函数
- SGU Inversions(线段树)
- 构造可配置词法语法分析器生成器(中)
- android开发步步为营之27:上下文菜单ContextMenu的用法
- Phalcon Cookie管理
- emacs之org-mode的转接(Refiling)
- 定义函数
- CPU 实模式 保护模式 和虚拟8086模式
- 数据结构学习之递归和迭代
- 计算机图形学--乌龟程序2
- redhat yum 源配置
- android开发步步为营之28:自定义Menu