【问题标题】:BlackBerry - TreeField with CheckBoxField?BlackBerry - 带有 CheckBoxField 的 TreeField?
【发布时间】:2009-06-24 12:27:25
【问题描述】:

是否可以在 BlackBerry 中将 CheckBoxField 添加到 TreeField

如果是,我该怎么做?

【问题讨论】:

  • 您好,您使用的是 RIM api 还是 MIDP 之一?
  • RIM API...我可以在表单上添加树字段...但我想要那些带有复选框的多选...我该怎么做?

标签: user-interface blackberry checkbox java-me custom-controls


【解决方案1】:

ListBox CheckBoxes 相同的技巧:
alt text http://img441.imageshack.us/img441/5120/checkboxtree.jpg

class CBTreeField extends VerticalFieldManager implements TreeFieldCallback,
        DrawStyle {
    boolean[] mBooleanValues = new boolean[] {};
    String[] mStringValues = new String[] {};

    public boolean getNodeBooleanValue(int node) {
        return mBooleanValues[node];
    }

    public void setNodeBooleanValue(int node, boolean value) {
        mBooleanValues[node] = value;
    }

    TreeField mRootField = null;

    public CBTreeField(String rootString, boolean rootBoolean) {
        mRootField = new TreeField(this, TreeField.FOCUSABLE);
        add(mRootField);
        mStringValues = insertAt(mStringValues, 0, rootString);
        mBooleanValues = insertAt(mBooleanValues, 0, rootBoolean);
    }

    public int addSiblingNode(int previousSibling, String stringValue,
            boolean booleanValue, Object cookie) {
        int index = mRootField.addSiblingNode(previousSibling, cookie);
        mBooleanValues = insertAt(mBooleanValues, index, booleanValue);
        mStringValues = insertAt(mStringValues, index, stringValue);
        return index;
    }

    public int addChildNode(int parent, String stringValue,
            boolean booleanValue, Object cookie) {
        int index = mRootField.addChildNode(parent, cookie);
        mBooleanValues = insertAt(mBooleanValues, index, booleanValue);
        mStringValues = insertAt(mStringValues, index, stringValue);
        return index;
    }

    static boolean[] insertAt(boolean[] inArray, int index, boolean value) {
        int newLen = inArray.length + 1;
        boolean[] outArray = new boolean[newLen];
        for (int i = 0, j = 0; i < newLen; i++, j++) {
            outArray[i] = (i != index) ? inArray[j] : value;
            if (i == index)
                j++;
        }
        return outArray;
    }

    static String[] insertAt(String[] inArray, int index, String value) {
        int newLen = inArray.length + 1;
        String[] outArray = new String[newLen];
        for (int i = 0, j = 0; i < newLen; i++, j++) {
            outArray[i] = (i != index) ? inArray[j] : value;
            if (i == index)
                j++;
        }
        return outArray;
    }

    public void drawTreeItem(TreeField treeField, Graphics g, int node, int y,
            int width, int indent) {
        String check = String
                .valueOf(mBooleanValues[node] ? 
                Characters.BALLOT_BOX_WITH_CHECK : Characters.BALLOT_BOX);
        g.drawText(check, indent, y, DrawStyle.LEFT);
        g.drawText(mStringValues[node], indent + 20, y, DrawStyle.RIGHT
                | ELLIPSIS);
    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(new MenuItem("Change value", 0, 0) {
            public void run() {
                int node = mRootField.getCurrentNode();
                mBooleanValues[node] = !mBooleanValues[node];
                invalidate();
            }
        });
    }
}

使用示例:

class Scr extends MainScreen {
    public Scr() {
        CBTreeField tree = new CBTreeField("root", false);
        add(tree);

        int ch01 = tree.addChildNode(0, "child 0-1", true, null);
        int ch02 = tree.addChildNode(0, "child 0-2", false, null);
        int ch03 = tree.addChildNode(0, "child 0-3", false, null);
        int ch011 = tree.addChildNode(ch01, "child 0-1-1", false, null);
        int ch012 = tree.addChildNode(ch01, "child 0-1-2", true, null);
        int ch031 = tree.addChildNode(ch03, "child 0-3-1", true, null);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多