让view弹出菜单 onCreateContextMenu

Button btn = (Button)findViewById(R.id.btn);

registerForContextMenu(btn);//这句就可以让view对象监听长按弹出菜单事件了

 

 

2.覆写下面3个方法

 @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        menu.add("点击我");
        menu.add("哈哈哈");
        menu.addSubMenu("12324354");//这个被点击的时候onContextMenuClosed只会执行一次,而menu.add();添加的会执行两次
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
//        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
//        Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
    	System.out.println("item:" + item);

        return true;
    }

	@Override
	public void onContextMenuClosed(Menu menu) {
		// TODO Auto-generated method stub
		super.onContextMenuClosed(menu);
		System.out.println("onContextMenuClosed");
	}
 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(contextMenu)