Android 菜单(ContextMenu)

系统 2795 0

Android 菜单(ContextMenu)

 

Android 的ContextMenu,即上下文菜单。(类似电脑上的鼠标右键功能,选中某个元素,然后点右键,在弹出菜单上执行操作。)在手机上,通过长时间按住界面上的元素,就会出现事先设计好的上下文菜单。

 

主要用到的方法有:

    // 创建显示上下文菜单
setOnCreateContextMenuListener

//监听长按事件
setOnItemLongClickListener

//监听普通按键事件
setOnItemClickListener

//上下文菜单的选中事件
onContextItemSelected

//监听上下文菜单关闭事件
onContextMenuClosed
  
 

 


示例(ListView上下文菜单)

 


Android 菜单(ContextMenu)
 

Android 菜单(ContextMenu)
 

效果图

 

main_list_view.xml 配置

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView
    android:id="@+id/main_list_view_listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
</LinearLayout>
  

 

main_list_view_item.xml配置

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/RelativeLayout_Item"
	android:layout_width="fill_parent" android:layout_height="wrap_content"
	android:paddingBottom="5dip"
	>
	<TextView 
		android:id="@+id/main_list_view_item_item_text"
		android:layout_centerHorizontal="true" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:textSize="20px"
		android:layout_gravity="center"
		/>
</LinearLayout>
  

 

Activity代码

    package com.zhouzijing.android;

import java.util.ArrayList;
import com.zhouzijing.android.util.MenuEntity;
import com.zhouzijing.android.util.SimpleAdapterUtil;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Menu2 extends Activity {
	final Context context = this;
	//定义1个ListView
	private ListView listView = null;
	//定义ListView需要显示的数据
	private ArrayList<ArrayList<Object>> viewInfos = null;
	//定义上下文菜单
	final private static int CONTEXT_MENU_EDIT = 0;//菜单[编辑]的编号 
	final private static int CONTEXT_MENU_SHARE = 1;//菜单[分享]的编号 
	final private static int CONTEXT_MENU_DELETE = 2;//菜单[删除]的编号 
	final private static int CONTEXT_MENU_CANCEL = 3;//菜单[取消]的编号 
	private int itemLongClickSelected = 0;
    private MenuEntity[] contextMenus = {
    		new MenuEntity(0, CONTEXT_MENU_EDIT, 0, "编辑"),
    		new MenuEntity(0, CONTEXT_MENU_SHARE, 1, "分享"),
    		new MenuEntity(0, CONTEXT_MENU_DELETE, 2, "删除"),
    		new MenuEntity(0, CONTEXT_MENU_CANCEL, 3, "取消"),
    		};

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_list_view);
        //初始化要显示的内容
    	initListViewInfos();
        //显示ListView
        showListView();
        //显示ListView的上下文菜单
        showListViewContextMenu();
    }
	
	/**
	 * 初始化ListView需要显示的内容.
	 */
	public void initListViewInfos(){
		viewInfos = new ArrayList<ArrayList<Object>>();
		ArrayList<Object> countrys = new ArrayList<Object>();
		countrys.add("Austria");
		countrys.add("China");
		countrys.add("Egypt");
		countrys.add("France");
		countrys.add("Ireland");
		countrys.add("Japan");
		countrys.add("Spain");
		countrys.add("United Kingdom");
		countrys.add("United States");
		viewInfos.add(countrys);
	}
	
	/**
	 * 从ListView显示内容中删除1条记录.
	 */
	public void removeItem(int index){
		viewInfos.get(0).remove(index);
	}
	/**
	 * 从ListView显示内容中读取1条记录
	 * @param index
	 * @return
	 */
	public Object readItem(int index){
		return viewInfos.get(0).get(index);
	}
	
	/**
	 * 读取当前执行的菜单
	 * @param index
	 * @return
	 */
	public CharSequence readContextMenu(int index){
		for(int i=0;i<contextMenus.length;i++){
			if(contextMenus[i].getItemId()==index) return contextMenus[i].getTitle();
		}
		return "";
	}
	/**
     * 显示ListView.
     */
    public void showListView(){
    	final Context context = this;
    	
    	//定义适配器to参数
    	int[] to = {R.id.main_list_view_item_item_text};
    	int resource = R.layout.main_list_view_item;
    	
    	//生成适配器:使用自定义工具类 SimpleAdapterUtil.java来生成适配器
    	SimpleAdapter simpleAdapter = SimpleAdapterUtil.create(context,viewInfos,resource,to); 
    	
    	listView = (ListView)findViewById(R.id.main_list_view_listview);
    	listView.setAdapter(simpleAdapter);
    }
    /**
     * 显示ListView的上下文菜单
     */
    public void showListViewContextMenu(){
    	//创建上下文菜单
    	listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){

			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				menu.setHeaderTitle(readItem(itemLongClickSelected).toString());
				MenuEntity menuEntity = null;
				for(int i=0;i<contextMenus.length;i++){
					menuEntity = contextMenus[i];
					menu.add(menuEntity.getGroupId(),menuEntity.getItemId(),menuEntity.getOrder(),menuEntity.getTitle());
				}
			}
    	});
    	
    	//监听列表项目的长按事件
    	listView.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				itemLongClickSelected = position;
				Toast.makeText(context,"item["+position+":"+readItem(itemLongClickSelected).toString()+"]被长时间点击了.",Toast.LENGTH_SHORT).show();
				return false;
			}
    		
    	});
    	
    	//监听列表项目的普通点击事件
    	listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(context,"item["+position+":"+readItem(position).toString()+"]被点击了.",Toast.LENGTH_SHORT).show();
			}
    	});
    }

	@Override
	public boolean onContextItemSelected(MenuItem item) {//执行上下文菜单中的菜单
		int itemId = item.getItemId();
		Toast.makeText(context,"Context menu item["+itemId+":"+readContextMenu(itemId)+"] is Selected.",Toast.LENGTH_SHORT).show();
		if(CONTEXT_MENU_DELETE==itemId){//删除
			//当前选中的ListView中的item编号
			int index = itemLongClickSelected;
			//在当前ListView中删除选中的元素
			removeItem(index);
			//重新显示ListView
			showListView();
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public void onContextMenuClosed(Menu menu) {//关闭上下文菜单
		Toast.makeText(context,"Context Menu is Closed.",Toast.LENGTH_SHORT).show();
		super.onContextMenuClosed(menu);
	}
    
    
}
  
 

 

备注:

 

    private MenuEntity[] contextMenus = {
    		new MenuEntity(0, CONTEXT_MENU_EDIT, 0, "编辑"),
    		new MenuEntity(0, CONTEXT_MENU_SHARE, 1, "分享"),
    		new MenuEntity(0, CONTEXT_MENU_DELETE, 2, "删除"),
    		new MenuEntity(0, CONTEXT_MENU_CANCEL, 3, "取消"),
    		};
  

参考 关于菜单基础属性的实体类

 

    SimpleAdapter simpleAdapter = SimpleAdapterUtil.create(context,viewInfos,resource,to); 
  

参考 1个简单的SimpleAdapter工具类-SimpleAdapterUtil(版本二:数据格式为ArrayList<ArrayList<Object>>)

 

 

 

关于根据上下文菜单来获取当前选中的GridView(或ListView等)的元素

 

    		//OnCreateContextMenuListener
		gridPhoneBook.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				//获取上下文菜单点击的GridView中的元素
				AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
				int position = info.position;
				//设置上下文菜单的标题
				PhoneBook phoneBook = (PhoneBook)phoneBookAdapter.getItem(position);
				menu.setHeaderTitle(phoneBook.getName());
				//设置上下文菜单
				MenuEntity menuEntity;
				for(int i=0;i<CONTEXT_MENUS.length;i++){
					menuEntity = CONTEXT_MENUS[i];
					menu.add(menuEntity.getGroupId(), menuEntity.getItemId()
							,menuEntity.getOrder(),menuEntity.getTitle());
				}
			}
		}); 
	

/**
	 * 上下文菜单-具体菜单的执行操作.
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		
		//获取选中的GridView的元素
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		int position = info.position;
		PhoneBook phoneBook = (PhoneBook)phoneBookAdapter.getItem(position);
		//菜单ID
		int menuId = item.getItemId();
		switch(menuId){
		case CONTEXT_MENU_PHONE_BOOK_DELETE:
			doContextMenuOfDelete(phoneBook);
			break;
		}
		return super.onContextItemSelected(item);
	}
  
 

 

 

 

 

 

 

Android 菜单(ContextMenu)


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论