2010-04-16 25 views
5

Soy un principiante en la programación de BlackBerry, tengo que reemplazar en mi aplicación el menú predeterminado (cuando presiona el botón de menú) mediante un menú personalizado, horizontal. La mejor manera de describir es que quiero el mismo resultado que la aplicación para BlackBerry ... WeatherEyeBlackBerry - barra de herramientas del menú personalizado

alt text http://www.blackberrybing.com/resource/pics/201002/WeatherEye-OS-45.jpg

Sé cómo crear el menú por defecto, pero éste no tengo ni idea! Gracias,

Respuesta

10

Lo que se necesita hacer es:

  • crear SizebleVFManager (ContentManager) como una extensión de VerticalFieldManager
  • conjunto ancho de la pantalla y la altura = (altura de la pantalla - alto de menú) el tamaño de ContentManager
  • añadir a la pantalla de Contenido
  • crear HorizontalFieldManager (MenuManager)
  • crear BitmapButtonField (menubutton) como una exten sión de ButtonField
  • conjunto FieldChangeListeners a menuButtons
  • añaden a menuButtons MenuManager
  • añadir a la pantalla MenuManager

Muestra de SizebleVFManager:

class SizebleVFManager extends VerticalFieldManager 
{ 
    int mWidth = 0; 
    int mHeight = 0; 
    public SizebleVFM(int width, int height, long style) { 
     super(style); 
     mWidth = width; 
     mHeight = height; 
    } 

    public SizebleVFM(int width, int height) { 
     mWidth = width; 
     mHeight = height; 
    } 

    public int getPreferredWidth() { 
     return mWidth; 
    } 

    public int getPreferredHeight() { 
     return mHeight; 
    } 

    protected void sublayout(int width, int height) { 
     width = getPreferredWidth(); 
     height = getPreferredHeight(); 
     super.sublayout(width, height); 
     setExtent(width, height); 
    } 
} 

...

SizebleVFManager contentManager = 
    new SizebleVFManager(Display.getWidth(), Display.getHeight(), 
     VERTICAL_SCROLL|VERTICAL_SCROLLBAR); 

Ver también
sample of BitmapButtonField and Toolbar

PS, aunque es mejor usar menú estándar ...

ACTUALIZACIÓN

Si desea deshabilitar la función de menú por defecto, cancelar el menú keydown:

protected boolean keyDown(int keycode, int time) { 
    if(Keypad.KEY_MENU == Keypad.key(keycode)) 
    { 
     return true; 
    } 
    else 
    return super.keyDown(keycode, time); 
} 

ACTUALIZACIÓN

He instalado ese maravilloso weather application y entender esta muestra puede ser más parecidos con varias mejoras:

  • uso CyclicHFManager como una extensión de HorizontalFieldManager
  • mostrar/ocultar MenuManager el botón de menú del botón

CyclicHFManager es un administrador que se mantendrá enfocado en el mismo lugar visualmente y ejecutar todos los campos, en ciclo.Al igual que en BlackBerry - Custom centered cyclic HorizontalFieldManager

class CyclicHFManager extends HorizontalFieldManager { 
    int mFocusedFieldIndex = 0; 
    boolean mCyclicTurnedOn = false; 

    public void focusChangeNotify(int arg0) { 
     super.focusChangeNotify(arg0); 
     if (mCyclicTurnedOn) { 
      int focusedFieldIndexNew = getFieldWithFocusIndex(); 
      if (focusedFieldIndexNew != mFocusedFieldIndex) { 
       if (focusedFieldIndexNew - mFocusedFieldIndex > 0) 
        switchField(0, getFieldCount() - 1); 
       else 
        switchField(getFieldCount() - 1, 0); 
      } 
     } 
     else 
     { 
      mFocusedFieldIndex = getFieldWithFocusIndex(); 
     } 
    } 

    private void switchField(int prevIndex, int newIndex) { 
     Field field = getField(prevIndex); 
     delete(field); 
     insert(field, newIndex); 
    } 
} 

alt text http://img109.imageshack.us/img109/6176/toolbarj.jpg

y código de muestra entera:

abstract class AScreen extends MainScreen { 
    boolean mMenuEnabled = false; 
    SizebleVFManager mContentManager = null; 
    CyclicHFManager mMenuManager = null; 

    public AScreen() { 
     mContentManager = new SizebleVFManager(Display.getWidth(), Display 
       .getHeight(), VERTICAL_SCROLL | VERTICAL_SCROLLBAR); 
     add(mContentManager); 

     // mMenuManager = new CyclicHFManager(Display.getWidth(), 60); 
     mMenuManager = new CyclicHFManager(); 
     mMenuManager.setBorder(BorderFactory.createBevelBorder(new XYEdges(4, 
       0, 0, 0), new XYEdges(Color.DARKBLUE, 0, 0, 0), new XYEdges(
       Color.WHITE, 0, 0, 0))); 
     mMenuManager.setBackground(BackgroundFactory 
       .createLinearGradientBackground(Color.DARKBLUE, Color.DARKBLUE, 
         Color.LIGHTBLUE, Color.LIGHTBLUE)); 

     for (int i = 0; i < 10; i++) { 
      Bitmap nBitmap = new Bitmap(60, 60); 
      Graphics g = new Graphics(nBitmap); 
      g.setColor(Color.DARKBLUE); 
      g.fillRect(0, 0, 60, 60); 
      g.setColor(Color.WHITE); 
      g.drawRect(0, 0, 60, 60); 
      Font f = g.getFont().derive(Font.BOLD, 40); 
      g.setFont(f); 
      String text = String.valueOf(i); 
      g.drawText(text, (60 - f.getAdvance(text)) >> 1, (60 - f 
        .getHeight()) >> 1); 

      Bitmap fBitmap = new Bitmap(60, 60); 
      g = new Graphics(fBitmap); 
      g.setColor(Color.DARKBLUE); 
      g.fillRect(0, 0, 60, 60); 
      g.setColor(Color.GOLD); 
      g.drawRect(0, 0, 60, 60); 
      g.setFont(f); 
      g.drawText(text, (60 - f.getAdvance(text)) >> 1, (60 - f 
        .getHeight()) >> 1); 

      BitmapButtonField button = new BitmapButtonField(nBitmap, fBitmap, 
        fBitmap); 
      button.setCookie(String.valueOf(i)); 
      button.setPadding(new XYEdges(0, 18, 0, 18)); 

      button.setChangeListener(new FieldChangeListener() { 
       public void fieldChanged(Field field, int context) { 
        Dialog.inform("Button # " + (String) field.getCookie()); 
       } 
      }); 

      mMenuManager.add(button); 
     } 
    } 

    protected boolean keyDown(int keycode, int time) { 
     if (Keypad.KEY_MENU == Keypad.key(keycode)) { 
      if (mMenuManager.getManager() != null) { 
       delete(mMenuManager); 
       mMenuManager.mCyclicTurnedOn = false; 
       mContentManager.updateSize(Display.getWidth(), Display 
         .getHeight()); 
      } else { 
       add(mMenuManager); 
       mMenuManager.getField(2).setFocus(); 
       mMenuManager.mCyclicTurnedOn = true; 
       mContentManager.updateSize(Display.getWidth(), Display 
         .getHeight() 
         - mMenuManager.getHeight()); 
      } 
      return true; 
     } else 
      return super.keyDown(keycode, time); 
    } 
} 

class FirstScreen extends AScreen { 

    public FirstScreen() { 
     mContentManager.add(new LabelField("This is a first screen")); 
    } 
} 

public class ToolbarMenuApp extends UiApplication { 

    public ToolbarMenuApp() { 
     pushScreen(new FirstScreen()); 
    } 

    public static void main(String[] args) { 
     (new ToolbarMenuApp()).enterEventDispatcher(); 
    } 

} 
+0

respuesta impresionante, Max. Una vez más, haces todo lo posible por publicar una respuesta detallada con el código. Ojalá pudiera +5 votar mejor esto. :) –

+0

Gracias Mark! :) –

+0

¡Comenzaré a trabajar en este proyecto la próxima semana, pero lo que leí y lo intenté rápidamente se ve genial! ¡Es muy útil, gracias! Todavía necesitaré algunos ajustes para hacer lo que quiero (exactamente lo mismo que la aplicación WeatherEye de hecho), como tener el menú visible (habilitar) todo el tiempo, y la alerta funcionando (tengo una pantalla blanca en su lugar). Volveré aquí la semana que viene. Estoy seguro de que sí. Pero nuevamente, ¡GRACIAS! – Dachmt

Cuestiones relacionadas