android的上下文菜单---context menu

android的菜单有三种:

 

 

Options Menu
当用户按下menu button按钮时显示的菜单
Context Menu
       当用户长久按住屏幕,即被注册显示上下文菜单的视图时显示的菜单
Submenu
当用户按下一个菜单的某个选项时弹出的子菜单
现在演示一个上下文菜单。
main.xml布局文件:此布局文件定义了listview,用来显示一组列表,稍后上下文菜单将被注册到该listview,并且响应该listview。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/layout" 
	android:layout_width="fill_parent" android:layout_height="fill_parent">
	<ListView android:id="@+id/lv_list" android:layout_width="wrap_content"
		android:layout_height="fill_parent">
	</ListView>
</RelativeLayout>
item.xml布局文件,用来显示listview中的一个单独的控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/layout"
	android:layout_width="fill_parent" android:layout_height="fill_parent">
		<TextView android:id="@+id/tv" android:layout_width="wrap_content"
			android:layout_height="fill_parent"
			android:textSize="20dip"
			>
		</TextView>
</RelativeLayout>
 
 在layout下新建一个menu文件夹,在建一个menu.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:id="@+id/new_game" android:title="新游戏" />
	<item android:id="@+id/help" android:title="寻求帮助" />
	<item android:id="@+id/addnew" android:title="添加新游戏" />
	<item android:id="@+id/delete" android:title="删除游戏" />
</menu>
 主activity代码
package org.ynu.android;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MenuActivity extends Activity {

	private ListView lv;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        //如定义了一个listview控件
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
        HashMap<String, String> map1 = new HashMap<String, String>();
        map1.put("item", "第一个菜单项");
        HashMap<String, String> map2 = new HashMap<String, String>();
        map2.put("item", "第二个菜单项");
        HashMap<String, String> map3 = new HashMap<String, String>();
        map3.put("item", "第三个菜单项");
        HashMap<String, String> map4 = new HashMap<String, String>();
        map4.put("item", "第四个菜单项");
        HashMap<String, String> map5 = new HashMap<String, String>();
        map5.put("item", "第五个菜单项");
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        
        //初始化该控件
        lv=(ListView)findViewById(R.id.lv_list);
        
        //定义适配器 参数为该显示信息用的布局文件 以及显示信息用的控件,这里是一个textview控件
        SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item, new String[]{"item"}, new int[]{R.id.tv});
        lv.setAdapter(adapter);
        
        //注册上下文菜单显示用的view
        registerForContextMenu(this.lv);
    }

    //该方法在注册的view被被长按时创建该view的上下文菜单
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	//当菜单某个选项被点击时调用该方法
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterContextMenuInfo infor = (AdapterContextMenuInfo)item.getMenuInfo();
		switch(item.getItemId())
		{
		case R.id.help:
			Toast.makeText(this, "请求帮助", Toast.LENGTH_LONG).show();
			return true;
		case R.id.addnew:
			Toast.makeText(this, "添加新的", Toast.LENGTH_LONG).show();
			return true;
		case R.id.delete:
			Toast.makeText(this, "删除信息", Toast.LENGTH_LONG).show();
			return true;
		case R.id.new_game:
			Toast.makeText(this, "新游戏", Toast.LENGTH_LONG).show();
			return true;
			default:
				return super.onContextItemSelected(item);
		}
	}

	//当上下文菜单关闭时调用的方法
	@Override
	public void onContextMenuClosed(Menu menu) {
		// TODO Auto-generated method stub
		super.onContextMenuClosed(menu);
	}

}
 效果图
android的上下文菜单---context menu

你可能感兴趣的:(android)