标签:
上下文菜单大多数情况下都是为ListView中的Item添加的,长按2S左右跳出菜单,添加上下文菜单必须重写活动中的onCreateContextMenu()方法和onContextItemSelected()方法,前者创建后者监听。
当然也可以通过xml文件创建菜单.
private final int MENU_DEL = Menu.FIRST; private final int MENU_MESS = Menu.FIRST+1; public void onCreateContextMenu(ContextMenu menu,View view,ContextMenuInfo menuinfo){ menu.setHeaderTitle("先来这么几个"); menu.add(Menu.NONE,MENU_DEL,0,R.string.DELETE); menu.add(Menu.NONE,MENU_MESS,0,R.string.MESS); } public boolean onContextItemSelected(MenuItem item){ AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo(); Music ss = allsongs.get(info.position); File file = new File(ss.getFileUrl()); switch(item.getItemId()){ case MENU_DEL: deleteFile(file); break; case MENU_MESS: break; default: break; } return true; } public void deleteFile(File f) { if (f.isDirectory()) { File[] files = f.listFiles(); if (files != null && files.length > 0) { for (int i = 0; i < files.length; ++i) { deleteFile(files[i]); } } } f.delete(); }
标签:
原文地址:http://www.cnblogs.com/liboyan/p/4504254.html