简体   繁体   English

仅在longclick上打开或填充操作栏菜单-Android

[英]Open or inflate the Actionbar menu on longclick only - Android

I'm trying to make the actionbar menu (onCreateOptionsMenu) open ONLY on a long-click. 我试图使操作栏菜单(onCreateOptionsMenu)仅在长按时打开。 How would I achieve this? 我将如何实现? At the moment I have it working for just a single short press/click using the following code: 目前,使用以下代码,只需短按一下即可运行它:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // TODO: Only onlongclick should the menu be revealed
    getMenuInflater().inflate(R.menu.my_menu_id, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_home:
            open_home();
            return true;
        case R.id.menu_how_to:
            open_how_to();
            return true;
        case R.id.menu_rate:
            open_rate();
            return true;
        case R.id.menu_about:
            open_about();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

I'd like the menu to ONLY open for a long click (sort of like a hidden/trick feature). 我希望菜单长按才能打开(有点像隐藏/特技功能)。 I've looked into onLongClickListener but can't get it to work. 我已经研究过onLongClickListener,但无法使其正常工作。 Thanks for your help! 谢谢你的帮助! Really appreciate it. 真的很感激。

I ended up figuring this one out myself. 我最终自己弄清楚了这个。 There's 2 ways of doing it. 有两种方法可以做到这一点。 Either a context menu or a alert dialog menu. 上下文菜单或警报对话框菜单。 The alert dialog menu allow icons whereas the context menu does not. 警报对话框菜单允许使用图标,而上下文菜单则不允许。

See my answer here: Open Actionbar Menu on Longclick - Android 在这里查看我的答案: 在Longclick上打开操作栏菜单-Android

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM