2012-06-01 13 views
6

Ho lavorato con ActionBarSherlock di recente, e follwing vari tutorial, ho scritto questo codice per aggiungere elementi alla barra di azioneCome distinguere due clic di voci di menu in ActionBarSherlock?

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    menu.add("Refresh") 
     .setIcon(R.drawable.ic_action_refresh) 
     .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 


    menu.add("Search")// Search 
     .setIcon(R.drawable.ic_action_search) 
     .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 
     return true; 
} 

Tuttavia, non so come distinguere i due click.

Anche se ho scoperto che è necessario eseguire l'override di onOptionsItemSelected per gestire i clic e anche che un'istruzione switch può essere utilizzata per distinguere tra i clic, ma la maggior parte delle esercitazioni usa gli ID oggetto dai menu xml. Dal momento che non sto creando menu in xml, come posso distinguere i clic senza id.

+0

Avete qualche ragione speciale per non definire il menu in un file XML? Sarebbe molto più facile –

risposta

17
private static final int REFRESH = 1; 
private static final int SEARCH = 2; 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    menu.add(0, REFRESH, 0, "Refresh") 
     .setIcon(R.drawable.ic_action_refresh) 
     .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 


    menu.add(0, SEARCH, 0, "Search") 
     .setIcon(R.drawable.ic_action_search) 
     .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 
     return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case REFRESH: 
      // Do refresh 
      return true; 
     case SEARCH: 
      // Do search 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 
+0

: Grazie, per me lo stesso problema e ora setIcon() non funziona. Puoi aiutarmi – sherin

0

si può fare da TI non ci Id in onOptionsItemSelected ................ che può essere impostato anche qui

http://thedevelopersinfo.wordpress.com/2009/10/29/handling-options-menu-item-selections-in-android/

http://developer.android.com/reference/android/view/Menu.html#add(int, int, int, java.lang.CharSequence)

use 
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title) 

Since: API Level 1 
Add a new item to the menu. This item displays the given title for its label. 
Parameters 

groupId The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group. 
itemId Unique item ID. Use NONE if you do not need a unique ID. 
order The order for the item. Use NONE if you do not care about the order. See getOrder(). 
title The text to display for the item. 
Returns 

The newly added menu item. 
+0

sì ma quale id, non ho impostato alcun id, e quando sto aggiungendo le voci di menu, non esiste alcun metodo come .setid (stringa) –

+0

oh grazie, non lo provo non appena prendo le mani sul mio computer , ignorare quindi l'ultimo commento. –

1

Basta controllare seguente

http://developer.android.com/guide/topics/ui/actionbar.html 

, che contiene

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { <--- here you can get it 
     case android.R.id.home: 
      // app icon in action bar clicked; go home 
      Intent intent = new Intent(this, HomeActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(intent); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 
Problemi correlati