Android的code获取选项菜单编程? [英] Android code for getting Options Menu programmatically?

查看:100
本文介绍了Android的code获取选项菜单编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android的code获取选项菜单编程android系统中显示活动后??

Android code for getting Options Menu programmatically in android after displaying the Activity??

这可能吗?
在此先感谢!

Is this possible? Thanks in advance!!

推荐答案

我能够通过挂钩onAttachedToWindow()事件打开菜单。创建视图后,这会自动启动。我用下面的code:

I was able to open the menu by hooking the onAttachedToWindow() event. This would fire automatically after the view was created. I used the following code:

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    try {
        ((Activity) this).openOptionsMenu();        
    } catch (Exception ex) {
        Log.e("ERR", "Error: " + ex.getMessage());
    }
}

当我试图在的onCreate(...)或onPostCreate(...),打开选项菜单事件,我收到以下错误:无法添加窗口 - 令牌空无效是您的活动运行?似乎直到视图正在绘制和注册到该窗口的选项菜单不可用。

When I attempted to open the Option Menu in the "onCreate(...)" or "onPostCreate(...)" events, I received the following error: "Unable to add window -- token null is not valid; is your activity running?" It seems that the Option Menu is not available until the view is being drawn and registered to the Window.

顺便说一句,它可以是preferable通过挂钩onTouchEvent(运动事件)作为在打开选项菜单:

As an aside, it may be preferable to open the Options Menu by hooking the "onTouchEvent(Motion event)" as in:

@Override
public boolean onTouchEvent(MotionEvent event) {
    ((Activity) this).openOptionsMenu();
    return super.onTouchEvent(event);
}

这样,如果用户点击或挥笔活动显示的选项菜单。为了完善的讨论中,选项菜单可以通过下面的命令予以辞退:

This way, the Options Menu is displayed if the user taps or swipes the activity. To round out the discussion, the Option Menu may be dismissed via the following command:

((活动)mContext).closeOptionsMenu();

((Activity) mContext).closeOptionsMenu();

因此​​,选项菜单可以被翻转,使用下面的命令:

Thus, the Options Menu can be "toggled" by using the following commands:

    ((Activity) this).openOptionsMenu();
    ((Activity) this).closeOptionsMenu(); 

好了,我希望这有助于。

Well, I hope this helps.

这篇关于Android的code获取选项菜单编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆