BlackBerry - 自定义菜单工具栏

时间:2010-04-16 21:20:50

标签: user-interface blackberry menu custom-controls

我是BlackBerry编程的初学者,我需要在我的应用程序中用自定义菜单替换默认菜单(当你按下菜单按钮时)。最好的描述是我想要与BlackBerry的WeatherEye应用程序相同的结果......

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

我知道如何创建默认菜单,但这个我不知道! 谢谢,

1 个答案:

答案 0 :(得分:10)

您需要做的是:

  • 创建SizebleVFManager(contentManager)作为VerticalFieldManager的扩展名
  • 将显示宽度和高度=(显示高度 - 菜单高度)大小设置为contentManager
  • 将contentManager添加到屏幕
  • 创建Horizo​​ntalFieldManager(menuManager)
  • 创建BitmapButtonField(menuButton)作为ButtonField的扩展名
  • 将FieldChangeListeners设置为menuButtons
  • 将menuButtons添加到menuManager
  • 将menuManager添加到屏幕

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);

另见
sample of BitmapButtonField and Toolbar

PS尽管使用标准菜单更好......

<强>更新

如果要禁用默认菜单功能,请取消MENU keydown:

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

更新

我安装了那个精彩的weather application,并且理解这个示例可能更加相似,有几处改进:

  • 使用CyclicHFManager作为Horizo​​ntalFieldManager的扩展名
  • 在菜单按钮上显示/隐藏menuManager单击

CyclicHFManager是一个管理器,可以在视觉上将焦点集中在同一个地方并在周期内运行所有字段。就像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

整个代码示例:

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();
    }

}