【问题标题】:Android ContextMenu starts in the onCreate method?Android ContextMenu 在 onCreate 方法中启动?
【发布时间】:2010-09-01 15:27:08
【问题描述】:

是否可以在 onCreate 方法上启动上下文菜单?我知道这可能是糟糕的设计道德,但我有我的理由!我试过了:

registerForContextMenu(this.getCurrentFocus());

但它不起作用..那么有人有更好的想法吗?

提前非常感谢!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(this);
    layout.setLayoutParams(new
    LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    //Button button = new Button(this);
    //button.setLayoutParams(new
    //LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    //button.setText("my button");

    TextView text = new TextView(this);
    text.setLayoutParams(new
    LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    layout.addView(text);
    setContentView(layout);

    registerForContextMenu(text);
    openContextMenu(layout);

【问题讨论】:

    标签: android contextmenu oncreate


    【解决方案1】:

    您需要为某些小部件执行registerForContextMenu(),然后使用openContextMenu()。不过,我同意你的结论,即这是一个糟糕的设计。

    【讨论】:

    • 马克,我已经添加了我试图在上面的问题中实现的代码,但是上下文菜单仍然拒绝打开。如果我将上下文菜单附加到按钮单击事件,代码将完美运行!干杯
    • @Ally:registerForContextMenu()的参数需要和openContextMenu()的参数一致。
    • 嗯我试过 registerForContextMenu(layout) openContextMenu(layout) 和 registerForContextMenu(text) openContextMenu(text) 无济于事,它只是让我的应用程序崩溃...... LogCat告诉我活动空闲超时!嗯
    • @Ally:你有没有考虑过做其他事情,而不是在onCreate() 中打开上下文菜单?这是一个低于标准的用户界面,显然是不可能的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多