2010-04-16 50 views
5

Tôi là người mới bắt đầu trong lập trình BlackBerry, tôi cần thay thế trong ứng dụng của mình menu mặc định (khi bạn nhấn nút menu) bằng menu tùy chỉnh, nằm ngang. Điều tốt nhất để mô tả là tôi muốn kết quả tương tự như các ứng dụng dành cho BlackBerry WeatherEye ...BlackBerry - Thanh công cụ menu tùy chỉnh

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

tôi biết làm thế nào để tạo menu mặc định, nhưng điều này tôi không có ý tưởng! Cảm ơn bạn,

Trả lời

10

Những gì bạn sẽ cần phải làm là:

  • tạo SizebleVFManager (contentManager) như là một phần mở rộng của VerticalFieldManager
  • bộ chiều rộng hiển thị và height = (chiều cao hiển thị - Chiều cao menu) kích thước contentManager
  • thêm contentManager để sàng lọc
  • tạo HorizontalFieldManager (menuManager)
  • tạo BitmapButtonField (menuButton) như một exten sion của ButtonField
  • bộ FieldChangeListeners để menuButtons
  • thêm menuButtons để menuManager
  • thêm menuManager để sàng lọc

Mẫu 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); 

Xem thêm
sample of BitmapButtonField and Toolbar

PS mặc dù tốt hơn để sử dụng menu tiêu chuẩn của nó ...

CẬP NHẬT

Nếu bạn muốn vô hiệu hóa chức năng menu mặc định, hủy MENU KeyDown:

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

CẬP NHẬT

Tôi đã cài đặt mà tuyệt vời weather application và hiểu mẫu này có thể giống nhau hơn với một số cải tiến:

  • sử dụng CyclicHFManager như một phần mở rộng của HorizontalFieldManager
  • hiện/ẩn menuManager vào nút Menu bấm

CyclicHFManager là một người quản lý sẽ tập trung vào cùng một vị trí trực quan và chạy tất cả các trường trên, theo chu kỳ.Giống như trong 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

Và mẫu mã toàn bộ:

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

câu trả lời Awesome, Max. Một lần nữa bạn đi ra khỏi con đường của bạn để gửi một phản ứng chi tiết với mã. Ước gì tôi có thể +5 upvote này. :) –

+0

Cảm ơn Mark! :) –

+0

Tôi sẽ bắt đầu làm lại dự án này vào tuần tới, nhưng những gì tôi đọc và thử nhanh sẽ rất tuyệt! Nó rất hữu ích, cảm ơn bạn! Tôi vẫn cần một số điều chỉnh để thực hiện những gì tôi muốn (giống như ứng dụng WeatherEye App), như việc hiển thị menu (bật) mọi lúc, và cảnh báo hoạt động (thay vào đó tôi có màn hình trắng). Tôi sẽ quay lại đây vào tuần tới, tôi chắc chắn ahah. Nhưng một lần nữa, CẢM ƠN BẠN! – Dachmt

Các vấn đề liên quan