2012-05-06 145 views
19

我是新來使用Sherlock的ActionBar,我已經使它在我的應用程序中運行,我有一個項目在操作欄上,但我不知道如何使項目做點什麼,當它被點擊我得到的只是這個。ActionBar Sherlock菜單項OnClick

public boolean onCreateOptionsMenu(Menu menu) { 

    menu.add("Folder") 
     .setIcon(R.drawable.folder) 
     .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 

    return true; 
} 


public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
     //What do i write here? 
    return true; 

我希望你明白我的意思:)

編輯

嘿我做了一點點幫助,從這個thread,我找到了工作,我做了一些改動,在這裏是代碼! :DDD

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater(); 
    inflater.inflate(R.menu.menu, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection 
    switch (item.getItemId()) { 
     case R.id.search: 
      finish(); 
      return true; 
     case R.id.new_folder: 
      finish(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

回答

23

試試這個,它的工作原理:

public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { 
     com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater(); 
     inflater.inflate(R.layout.menu, menu); 
} 

public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     switch (item.getItemId()) { 
     case R.id.settings: 
      Intent i=new Intent(class1.this, clas2.class); 
      startActivity(i); 
      return true; 
     } 
     return false; 
} 
+0

onActionItemclick,如果我不想開始新的活動,但想膨脹另一個視圖,那麼我必須使用?我應該使用Fragment嗎?我實施片段但是視圖不是誇大..所以Plz幫助我.. –

8

類似的東西(取自我的一個應用程序)。 這種情況下的活動延伸SherlockFragmentActivity

@Override 
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { 
    menu.add(Menu.NONE, R.id.ID_ACTION_EXIT, Menu.NONE,R.string.action_label_exit) 
    .setIcon(android.R.drawable.ic_menu_close_clear_cancel) 
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(
     com.actionbarsherlock.view.MenuItem item) { 
    switch(item.getItemId()){ 
    case R.id.ID_ACTION_EXIT: 
     Main.this.finish(); 
     return true; 
    default: 
     return false; 
    } 
} 

R.id.ACTION_EXIT置於res/values/ids.xml

<resources><item name="ID_ACTION_EXIT" type="id"/></resources> 

[編輯] 嘗試這種然後(延伸SherlockListActivity)。你必須先做一些編輯。這段代碼來自Sherlock演示。你應該下載它們。

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    SubMenu sub = menu.addSubMenu("Theme"); 
    sub.add(0, R.style.Theme_Sherlock, 0, "Default"); 
    sub.add(0, R.style.Theme_Sherlock_Light, 0, "Light"); 
    sub.add(0, R.style.Theme_Sherlock_Light_DarkActionBar, 0, "Light (Dark Action Bar)"); 
    sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (item.getItemId() == android.R.id.home || item.getItemId() == 0) { 
     return false; 
    } 
    THEME = item.getItemId(); 
    Toast.makeText(this, "Theme changed to \"" + item.getTitle() + "\"", Toast.LENGTH_SHORT).show(); 
    return true; 
} 
+0

我測試,但沒有任何反應 –

+0

最後的帖子是用另一個例子編輯的。 – ernell

+0

我修好了。代碼在問題:) –

2

請過嘗試。

import android.os.Bundle; 
import android.widget.Toast; 

import com.actionbarsherlock.app.SherlockActivity; 
import com.actionbarsherlock.view.MenuInflater; 
import com.actionbarsherlock.view.MenuItem; 

public class ActionBarTestActivity extends SherlockActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_action_bar_test); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { 

     MenuInflater inflater = getSupportMenuInflater(); 
     inflater.inflate(R.menu.activity_action_bar_test, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     super.onOptionsItemSelected(item); 

     switch (item.getItemId()) { 
     case R.id.phone: 
      Toast.makeText(getBaseContext(), "You selected Phone", 
        Toast.LENGTH_SHORT).show(); 
      break; 

     case R.id.computer: 
      Toast.makeText(getBaseContext(), "You selected Computer", 
        Toast.LENGTH_SHORT).show(); 
      break; 

     case R.id.gamepad: 
      Toast.makeText(getBaseContext(), "You selected Gamepad", 
        Toast.LENGTH_SHORT).show(); 
      break; 

     case R.id.camera: 
      Toast.makeText(getBaseContext(), "You selected Camera", 
        Toast.LENGTH_SHORT).show(); 
      break; 

     case R.id.video: 
      Toast.makeText(getBaseContext(), "You selected Video", 
        Toast.LENGTH_SHORT).show(); 
      break; 

     case R.id.email: 
      Toast.makeText(getBaseContext(), "You selected EMail", 
        Toast.LENGTH_SHORT).show(); 
      break; 

     } 
     return true; 
    } 
} 
+0

onActionItemclick,如果我想膨脹另一個視圖然後我必須使用?我應該使用Fragment嗎?我實施片段但是觀點沒有膨脹..所以Plz幫助我。 –

0
You use this one 

public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { 
     getSherlockActivity().getSupportMenuInflater().inflate(R.menu.settingmenu, menu); 
    getSherlockActivity().getSupportMenuInflater().inflate(R.menu.menugridcalendar,me 
} 

This will be added in your sherlockfragment activity: 


public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.settings: 
      Intent intent=new Intent(currentclass.this, calledclass.class); 
      startActivity(intent); 
      return true; 
     } 
     return false; 
}