Register Action Mode// Register the action mode registerForContextMenu (btnView) for Btn; // The response duration is based on the event btn. setOnLongClickListener (new OnLongClickListener () {@ Override public boolean onLongClick (View v) {v. setSelected (true); ContextMenu. this. startActionMode (mActionMode); return true ;}});
2. Implement the Action Mode callback Interface
/***** Create callback interface **/private ActionMode. callback mActionMode = new ActionMode. callback () {@ Override public boolean onPrepareActionMode (ActionMode mode, Menu menu) {// TODO Auto-generated method stub return false;} @ Override public void onDestroyActionMode (ActionMode mode) {// TODO Auto-generated method stub if (mode! = Null) {mode = null ;}@ Override public boolean onCreateActionMode (ActionMode mode, Menu menu) {MenuInflater inflater = getMenuInflater (); // Add the Menu file inflater. inflate (R. menu. main_icon, menu); return true ;}@ Override public boolean onActionItemClicked (ActionMode mode, MenuItem item) {switch (item. getItemId () {case R. id. action_share: Log. d (tag, action_share); Toast. makeText (getApplicationContext (), action_share, Toast. LENGTH_SHORT ). show (); mode. finish (); break; case R. id. action_save: Log. d (tag, action_save); Toast. makeText (getApplicationContext (), action_save, Toast. LENGTH_SHORT ). show (); mode. finish (); break; case R. id. action_settings: Log. d (tag, action_settings); Toast. makeText (getApplicationContext (), action_settings, Toast. LENGTH_SHORT ). show (); mode. finish (); break; case R. id. action_delete: Log. d (tag, action_delete); Toast. makeText (getApplicationContext (), action_delete, Toast. LENGTH_SHORT ). show (); mode. finish (); break; case R. id. action_edit: Log. d (tag, action_edit); Toast. makeText (getApplicationContext (), action_edit, Toast. LENGTH_SHORT ). show (); mode. finish (); break; default: break;} return true ;}};
3. cancel registration action mode
// Cancel registration of unRegisterForContextMenu (btnView );
XML file of Menu
In addition, the action mode can be combined with ListView and GridView.
Long-pressed ListView Item has the following effect:
The CSDN cannot upload the image.
Code Implementation
ListView. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE_MODAL); listView. setMultiChoiceModeListener (new MultiChoiceModeListener () {@ Override public boolean onPrepareActionMode (ActionMode mode, Menu menu) {// TODO Auto-generated method stub return false ;} @ Override public void onDestroyActionMode (ActionMode mode) {// TODO Auto-generated method stub} @ Override public boolean onCreateActionMode (ActionMode mode, Menu menu) {MenuInflater inflater = getMenuInflater (); inflater. inflate (R. menu. main_icon, menu); return true ;}@ Override public boolean onActionItemClicked (ActionMode mode, MenuItem item) {switch (item. getItemId () {case R. id. action_share: Log. d (tag, action_share); // Modify the title mode. setTitle (action_share); Toast. makeText (getApplicationContext (), action_share, Toast. LENGTH_SHORT ). show (); mode. finish (); break; case R. id. action_save: Log. d (tag, action_save); Toast. makeText (getApplicationContext (), action_save, Toast. LENGTH_SHORT ). show (); mode. finish (); break; default: break;} return true;} @ Override public void onItemCheckedStateChanged (ActionMode mode, int position, long id, boolean checked) {// TODO Auto-generated method stub }});
Ii. Floating Mode
- Create menu
- Set Menu listening events
Specific Code Process
@ Overridepublic void onCreateContextMenu (android. view. contextMenu menu, View v, ContextMenuInfo menuInfo) {MenuInflater inflater = getMenuInflater (); inflater. inflate (R. menu. main_icon, menu); super. onCreateContextMenu (menu, v, menuInfo) ;}@ Overridepublic boolean onContextItemSelected (MenuItem item) {// only partial response switch (item. getItemId () {case R. id. action_compass: break; case R. id. action_delete: break; default: break;} return super. onContextItemSelected (item );}
The XML file of Menu is the same as the action mode.
Although the Menu has icons, ContextMenu does not support icons, which are described on the official website.
In this way, the basic ContextMenu floating mode menu is completed.
Other methods related to ContextMenu
/***** Open the menu ***/@ Overridepublic void openContextMenu (View view) {// TODO Auto-generated method stub super. openContextMenu (view);}/*** close menu ***/@ Overridepublic void closeContextMenu () {// TODO Auto-generated method stub super. closeContextMenu ();}/*** listener event after the Menu is closed ***/@ Overridepublic void onContextMenuClosed (menu Menu) {super. onContextMenuClosed (menu );}