2010-09-01 54 views
0

是否可以在onCreate方法上啓動上下文菜單?我知道它可能是壞的設計道德,但我有我的答覆!我試過了:Android ContextMenu從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); 

回答

0

您需要爲某些部件做registerForContextMenu(),然後使用openContextMenu()。但是,我同意你的結論,這是一個糟糕的設計。

+0

馬克,我已經添加了代碼即時嘗試在上述問題中實現,但上下文菜單仍然拒絕打開。如果我將上下文菜單附加到按鈕單擊事件上,代碼完美地工作! Cheers – Ally 2010-09-01 20:03:07

+0

@Ally:registerForContextMenu()的參數需要與openContextMenu()的參數相同。 – CommonsWare 2010-09-01 20:06:31

+0

嗯我試過registerForContextMenu(佈局)openContextMenu(佈局)和registerForContextMenu(文本)openContextMenu(文本)沒有用它的只是崩潰我的應用程序... LogCat告訴我活動空閒超時!嗯 – Ally 2010-09-01 20:20:43

相關問題