Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of long-press pop-up context menu in Android

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report