Android - EditTexts in Gallery show strange behaviour when being (long)-clicked

醉酒当歌 提交于 2019-11-29 16:51:23

From Google's documentation:

public void registerForContextMenu (View view)

Registers a context menu to be shown for the given view (multiple views can show the context menu). This method will set the View.OnCreateContextMenuListener on the view to this activity, so onCreateContextMenu(ContextMenu, View, ContextMenuInfo) will be called when it is time to show the context menu.

As you can see from the documentation, onCreateContextMenu() will be called in Main before the context menu is shown. You will need to override this method to create your custom context menu.

m1ntf4n

Thanks, glorifiedHacker. You led me to the following solution:

  1. In the activity's onCreate() method, we require a registerForContext(myGallery).

  2. Create your own class MyEditText that extends EditText. In this class (after adding the constructors, of course), you need to make the protected method EditText::onCreateContextMenu() accessible:

    @Override
    public void onCreateContextMenu(ContextMenu menu) {
        super.onCreateContextMenu(menu);
    }
    
  3. Back in the activity, do

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View v, 
                                    ContextMenu.ContextMenuInfo menuInfo) 
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        ((MyEditText) info.targetView).onCreateContextMenu(contextMenu);
    }
    

    The first line is getting the view which is requesting a context menu, the second one is calling the (now visible) onCreateContextMenu() of the EditText, so on a long press the contextmenu the user is used to appears.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!