How to implement context menu on key press instead of long click/tap

前端 未结 2 490
梦如初夏
梦如初夏 2020-12-16 03:18

I have a ListActivity and I want to implement context menu for each of the list elements. I know that the common way to do this is to show the context menu on long click/tap

相关标签:
2条回答
  • 2020-12-16 03:49

    To open the context menu, call openContextMenu(). To trigger it via a key, override onKeyDown() or onKeyUp().

    That being said, I really do not recommend this.

    Users complain that Android has no UI standards. Instead, Android has UI conventions, ones that allow developers some freedom (and, more importantly, are not barriers to getting your app listed on the Android Market).

    However, those users' point is very valid -- their experience is marred when apps decide to go off on a UI tangent. Deciding to have the MENU key pop a context menu would be one such tangent. For starters, on touch-screen devices, this will not work very well, because there is no selected item in your ListView, so it would be unclear to the user what the MENU pertains to.

    I suspect that there is a better solution for whatever problem you think you are solving this way.

    0 讨论(0)
  • 2020-12-16 03:59

    I have actually done this same thing for one of my apps because it made sense for my app. The best and easiest way to do this is to override onListItemClick() for your listActivity. This is better than onKeyDown() because it pertains specifically to the list item (view) in question and will only apply to the list items, and not the rest of your screen.

    onListItemClick(ListView l, View v, int position, long id)
    {
       v.showContextMenu();
    }
    

    then all you have to do override the context menu methods and you're golden

    0 讨论(0)
提交回复
热议问题