2011-06-13 30 views

回答

4

您必須重寫Activity中的onOptionsItemSelected方法,該方法在用戶單擊「選項」菜單中的項目時調用。在該方法中,您可以檢查點擊了哪個項目。

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
    case R.id.menu_item1: 
     Intent intent = new Intent(this, ActivityForItemOne.class); 
     this.startActivity(intent); 
     break; 
    case R.id.menu_item2: 
     // another startActivity, this is for item with id "menu_item2" 
     break; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 

    return true; 
} 

也有它的工作原理與之相似onContextItemSelected方法,但對於上下文菜單(我不知道,什麼菜單你的意思)。在 http://developer.android.com/guide/topics/ui/menus.html

類意向

更多信息 - 動作和類別常量http://developer.android.com/reference/android/content/Intent.html

Action元素 http://developer.android.com/guide/topics/manifest/action-element.html

意圖和意圖過濾器 http://developer.android.com/guide/topics/intents/intents-filters.html

希望這會解決您的問題。

學分ICEMAN,How to call Activity from a menu item in Android?

0

This能對您有所幫助。

+0

如果你表明他特別應該尋找什麼,這將是更有幫助 - 像「創建選項」菜單或鏈接他像HTTP教程://kahdev.wordpress。 com/2008/11/25/building-a-menu-for-your-android-v10-r1-app /也是如此。 – jkhouw1 2011-06-13 20:49:09

相關問題