黑莓手机 - 如何创建子菜单? [英] BlackBerry - How to create sub menu?

查看:124
本文介绍了黑莓手机 - 如何创建子菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为BB应用程序创建子菜单
当我在菜单项点击它显示

i want to create sub menu for a BB application when i click on menu item it shows

Option 1
Option 2
Option 3

当我在选项3单击它应该显示

When i click on option 3 it should display

1
2
3

作为子菜单项。

推荐答案

一直想做到这一点)结果

Always wanted to do this )

class Scr extends MainScreen {
    SubMenu menu = new SubMenu();

    public Scr() {

        for (int i = 0; i < 3; i++) {
            SubMenu sMenu = new SubMenu();
            menu.add(new SubMenuItem(i + " item", sMenu));
            for (int k = 0; k < 3; k++) {
                SubMenu sSMenu = new SubMenu();
                sMenu.add(new SubMenuItem(i + "-" + k + " item", sSMenu));
                for (int l = 0; l < 3; l++) {
                    sSMenu
                            .add(new SubMenuItem(i + "-" + k + "-" + l
                                    + " item"));
                }
            }
        }

        add(new LabelField("testing menu", FOCUSABLE) {
            protected void makeContextMenu(ContextMenu contextMenu) {
                menu.mRectangle.x = this.getContentRect().X2();
                menu.mRectangle.y = this.getContentRect().Y2();
                Ui.getUiEngine().pushScreen(menu);

                EventInjector.invokeEvent(new KeyEvent(KeyEvent.KEY_DOWN,
                        Characters.ESCAPE, 0));
            }
        });
    }
}

class SubMenu extends PopupScreen implements ListFieldCallback {
    private static final int MAX_WIDTH = Font.getDefault().getAdvance(
            "max menu item text");
    XYRect mRectangle = new XYRect();
    Vector mSubMenuItems = new Vector();
    ListField mListField;

    public SubMenu() {
        super(new SubMenuItemManager(), DEFAULT_CLOSE);
        int rowHeight = getFont().getHeight() + 2;
        mListField = new ListField() {
            protected boolean navigationClick(int status, int time) {
                runMenuItem(getSelectedIndex());
                return super.navigationClick(status, time);
            }
        };
        mListField.setRowHeight(rowHeight);
        add(mListField);
        mListField.setCallback(this);
        updateMenuItems();
    }

    public void add(SubMenuItem subMenuItem) {
        mSubMenuItems.addElement(subMenuItem);
        subMenuItem.mMenu = this;
        updateMenuItems();
    }

    private void updateMenuItems() {
        int rowCounts = mSubMenuItems.size();
        mRectangle.width = getMaxObjectToStringWidth(mSubMenuItems);
        mRectangle.height = mListField.getRowHeight() * rowCounts;
        mListField.setSize(rowCounts);
    }

    private void runMenuItem(int index) {
        SubMenuItem item = (SubMenuItem) get(mListField, index);
        if (null != item.mSubMenu) {
            item.mSubMenu.setSubMenuPosition(getSubMenuRect(index));
            Ui.getUiEngine().pushScreen(item.mSubMenu);
        } else {
            item.run();
        }
    }

    private XYRect getSubMenuRect(int index) {
        SubMenuItem item = (SubMenuItem) get(mListField, index);
        XYRect result = item.mSubMenu.mRectangle;
        result.x = mRectangle.x + mRectangle.width;
        result.y = mRectangle.y + mListField.getRowHeight() * index;
        int testWidth = Display.getWidth() - (mRectangle.width + mRectangle.x);
        if (testWidth < result.width) {
            result.width = testWidth;
        }

        int testHeight = Display.getHeight()
                - (mRectangle.height + mRectangle.y);
        if (testHeight < result.height)
            result.height = testHeight;

        return result;
    }

    public void setSubMenuPosition(XYRect rect) {
        mRectangle = rect;
    }

    protected void sublayout(int width, int height) {
        super.sublayout(mRectangle.width, mRectangle.height);
        setPosition(mRectangle.x, mRectangle.y);
        setExtent(mRectangle.width, mRectangle.height);
    }

    private int getMaxObjectToStringWidth(Vector objects) {
        int result = 0;
        for (int i = 0; i < objects.size(); i++) {
            int width = getFont().getAdvance(objects.elementAt(i).toString());
            if (width > result) {
                if (width > MAX_WIDTH) {
                    result = MAX_WIDTH;
                    break;
                } else {
                    result = width;
                }
            }
        }
        return result;
    }

    public void drawListRow(ListField field, Graphics g, int i, int y, int w) {
        // Draw the text.
        String text = get(field, i).toString();
        g.setColor(Color.WHITE);
        g.drawText(text, 0, y, DrawStyle.ELLIPSIS, w);
    }

    public Object get(ListField listField, int index) {
        return mSubMenuItems.elementAt(index);
    }

    public int getPreferredWidth(ListField listField) {
        return mRectangle.width;
    }

    public int indexOfList(ListField listField, String prefix, int start) {
        return 0;
    }
}

class SubMenuItemManager extends VerticalFieldManager {
    public SubMenuItemManager() {
        super(USE_ALL_HEIGHT | USE_ALL_WIDTH);
    }

    public int getPreferredWidth() {
        return ((SubMenu) getScreen()).mRectangle.width;
    }

    public int getPreferredHeight() {
        return ((SubMenu) getScreen()).mRectangle.height;
    }

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

}

class SubMenuItem implements Runnable {
    String mName;
    SubMenu mMenu;
    SubMenu mSubMenu;

    public SubMenuItem(String name) {
        this(name, null);
    }

    public SubMenuItem(String name, SubMenu subMenu) {
        mName = name;
        mSubMenu = subMenu;
    }

    public String toString() {
        return mName;
    }

    public void run() {

    }
}

这篇关于黑莓手机 - 如何创建子菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆