How to realize the function of long-press pop-up context menu in Android
This article mainly introduces the Android how to achieve the long press pop-up context menu function related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you read this Android how to achieve long press pop-up context menu function article will have a harvest, let's take a look at it.
Step 1: register the context menu for a control in the right place in the program
Components can be buttons, text boxes, or list entries. Take the listView list as an example
ListView contentList= (ListView) findViewById (R.id.blackname_manager_listV); contentList.setAdapter (mListAdapter); registerForContextMenu (contentList)
Step 2: override the onCreateContextMenu method in activity and add menu items
Public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {super.onCreateContextMenu (menu, v, menuInfo); menu.add (0, MENU_UPDATE, 0, "modify information"); menu.add (0, MENU_ADD, 0, "Delete record");}
Step 3: override the onContextItemSelected method in activity to handle menu item events
Public boolean onContextItemSelected (MenuItem item) {/ / get the context menu adapter AdapterContextMenuInfo cmi= (AdapterContextMenuInfo) item.getMenuInfo (); / / get the selected menu location int posMenu=cmi.position; / / associate the menu item with the list view entry items= (BlackNumber) mListAdapter.getItem (posMenu); switch (item.getItemId ()) {case MENU_UPDATE:// executes the business logic break of the menu item Case MENU_ADD: / / the business logic that executes the menu item break;} return super.onContextItemSelected (item);} this article ends here on "how to implement the long-press pop-up context menu in Android". Thank you for reading! I believe that everyone has a certain understanding of "how to achieve the function of long-press pop-up context menu in Android". If you want to learn more, you are welcome to follow the industry information channel.