2012-06-01 18 views
6

que han estado trabajando con ActionBarSherlock recientemente, y follwing varios tutoriales, escribí este código para añadir elementos a la barra de acciones¿Cómo distinguir dos clics en elementos de menú en 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; 
} 

Sin embargo, no sé cómo distinguir los dos clics.

Aunque descubrí que debe sobrescribir onOptionsItemSelected para manejar los clics y también que se puede usar una declaración de interruptor para distinguir entre clics, pero la mayoría de los tutoriales usan ids de elementos de sus menús xml. Como no estoy creando menús en xml, ¿cómo puedo distinguir los clics sin identificadores?

+0

¿Tiene usted alguna razón especial para no definir el menú en un archivo XML? Sería mucho más fácil. –

Respuesta

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

: Gracias, para mí, el mismo problema y ahora setIcon() no funciona. ¿Pueden ayudarme? – sherin

0

que puede hacer por ti no Id en onOptionsItemSelected ................ que se puede establecer aquí también

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í, pero qué id, no les he establecido ninguna identificación, y cuando agrego los elementos del menú, no hay ningún método como .setid (cadena) –

+0

oh gracias, lo intentaré tan pronto como tenga manos en mi computadora , ignora el último comentario entonces. –

1

Sólo echa siguiente

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

que 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); 
    } 
} 
Cuestiones relacionadas