How to use ContextMenu menu.

隗驰
2023-12-01

使用Context Menu方法。

1.在onCreateContextMenu中创建菜单。

2.在onContextItemSelected中填写菜单响应处理

3.使用registerForContextMenu 注册菜单到指定listview。 

4.调用showContextMenu显示菜单


public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) { 
		case MENU_SET_AMOUNT:
			
			break;
		case MENU_SHOW_DETAILS: 
			
			break; 
		case MENU_DELETE_ITEM: 
			
			break; 
		default: 
			break; 
		} 
		return super.onContextItemSelected(item);
	}


	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		
		menu.setHeaderTitle(R.string.popup_menu_title);
		menu.add(0, MENU_SET_AMOUNT, 0, R.string.menu_set_amount);
		menu.add(0, MENU_SHOW_DETAILS, 0, R.string.menu_show_details);
		menu.add(0, MENU_DELETE_ITEM, 0, R.string.menu_delete_item);		

		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;

	}

	class MyOnItemClickListener implements AdapterView.OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View v, int position,
				long arg3) {

			selectListItemNum = position;
			v.showContextMenu();
		}
	}
registerForContextMenu(breakfastListView);

breakfastListView.setOnItemClickListener(new MyOnItemClickListener());


 类似资料:

相关阅读

相关文章

相关问答