2016-07-23 75 views
-1

而不是使用吐司,我想用在菜單項中的點擊監聽器,並且我們可以在這種情況下使用片段 這是我想添加點擊,下面的代碼,所以我可以打開一個新的活動如何使用點擊監聽器上的菜單項?

//啓動菜單XML文件(menu.xml文件)

@Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     MenuInflater menuInflater = getMenuInflater(); 
     menuInflater.inflate(R.layout.menu, menu); 
     return true; 
    } 

    /** 
    * Event Handling for Individual menu item selected 
    * Identify single menu item by it's id 
    * */ 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 

     switch (item.getItemId()) 
     { 
     case R.id.menu_bookmark: 
      // Single menu item is selected do something 
      // Ex: launching new activity/screen or show alert message 
      Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show(); 
      return true; 

     case R.id.menu_save: 
      Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show(); 
      return true; 

     case R.id.menu_search: 
      Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show(); 
      return true; 

     case R.id.menu_share: 
      Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show(); 
      return true; 

     case R.id.menu_delete: 
      Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show(); 
      return true; 

     case R.id.menu_preferences: 
      Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show(); 
      return true; 

     default: 
      return super.onOptionsItemSelected(item); 
     } 
    }  

} 
+0

可能的複製[如何啓動活動在Android 2.1](http://stackoverflow.com/questions/3720581/how-to-start-activity-in-android-2-1) – Divers

回答

1

是的,你可以做到這一點通過簡單的開關殼體的內部添加一個Intent對每個菜單項。有供您參考在下面摘錄一看:

@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 

    switch (item.getItemId()) 
    { 
    case R.id.menu_bookmark: 

     // Ex: launching new activity/screen or show alert message 

     Intent intent = new Intent(yourActivity.this, NextActivity.class) 
     startActivity(intent); 
     return true; 

    default: 
     return super.onOptionsItemSelected(item); 
    } 
}  
+0

謝謝mayank,它的工作 –

0
@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 

    switch (item.getItemId()) 
    { 
    case R.id.menu_bookmark: 

     Intent intent = new Intent(currentactivity.this, toactivity.class) 
     startActivity(intent); 

     return true; 

    case R.id.menu_save: 
     Intent intent = new Intent(currentactivity.this, toactivity.class) 
     startActivity(intent); 
     return true; 

    case R.id.menu_search: 
     Intent intent = new Intent(currentactivity.this, toactivity.class) 
     startActivity(intent); 
     return true; 

    case R.id.menu_share: 
     Intent intent = new Intent(currentactivity.this, toactivity.class) 
     startActivity(intent); 
     return true; 

    case R.id.menu_delete: 
     Intent intent = new Intent(currentactivity.this, toactivity.class) 
     startActivity(intent); 
     return true; 

    case R.id.menu_preferences: 
     Intent intent = new Intent(currentactivity.this, toactivity.class) 
     startActivity(intent); 
     return true; 

    default: 
     return super.onOptionsItemSelected(item); 
    } 
}  

意向 你必須使用意圖從一個屏幕移動到另一個

Intent intent = new Intent(currentactivity.this,towhichactivityyouwantmove.class) 
    startActivity(intent);