在blackberry java中扩展一个字段,而其他字段则被折叠

在blackberry java中扩展一个字段,而其他字段则被折叠,java,eclipse,blackberry,Java,Eclipse,Blackberry,我正在开发一个应用程序,在这个应用程序中,我根据将标志设置为true或false来添加和删除字段。但我试图做的是,如果我单击字段,则该特定字段应该展开,而其他字段则折叠,即使该字段已展开 我在谷歌上搜索了一下,但没有找到解决方案。请帮助我,我是黑莓java新手 我使用了下面的代码 public final class MyScreen extends MainScreen implements FieldChangeListener { /** * Creates a ne

我正在开发一个应用程序,在这个应用程序中,我根据将标志设置为true或false来添加和删除字段。但我试图做的是,如果我单击字段,则该特定字段应该展开,而其他字段则折叠,即使该字段已展开 我在谷歌上搜索了一下,但没有找到解决方案。请帮助我,我是黑莓java新手

我使用了下面的代码

 public final class MyScreen extends MainScreen implements 
FieldChangeListener
{
    /**
     * Creates a new MyScreen object
     */
    private VerticalFieldManager main_manager;
    private HorizontalFieldManager parentNodes;
    private LabelField parent_lables[];
    private Bitmap bitmap,upbitmap;
    private BitmapField bitmap_field[];
    private VerticalFieldManager submanager[];
    private int sizeOfParentNodes=3;
    private int sizeOfChildNodes=5;
    private static boolean flag[];

    public MyScreen()
    {        
        // Set the displayed title of the screen       
        bitmap=Bitmap.getBitmapResource("arrow.png");
        upbitmap=Bitmap.getBitmapResource("uparrow.png");
        main_manager=new  
     VerticalFieldManager(Manager.VERTICAL_SCROLL|VERTICAL_SCROLLBAR){
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(Display.getWidth(), Display.getHeight());
                setExtent(Display.getWidth(), Display.getHeight());
            };
        };
        parent_lables=new LabelField[sizeOfParentNodes];
        flag=new boolean[sizeOfParentNodes];
        submanager=new VerticalFieldManager[sizeOfParentNodes];
        bitmap_field=new BitmapField[sizeOfParentNodes];
        for(int i=0;i<sizeOfParentNodes;i++)
        {
            submanager[i]=new VerticalFieldManager();
            updateGUI(i);
            main_manager.add(submanager[i]);
        }
        add(main_manager);
    }

    public void fieldChanged(Field field, int context) {
        // TODO Auto-generated method stub
        synchronized (UiApplication.getEventLock()) {
            for(int i=0;i<sizeOfParentNodes;i++)
            {   if(field==parent_lables[i])
                {
                    if(flag[i]==true){
                        flag[i]=false;
                        submanager[i].deleteAll();
                        updateGUI(i);
                        parent_lables[i].setFocus();
                    }else{
                        flag[i]=true;
                        bitmap_field[i].setBitmap(upbitmap);

                        submanager[i].invalidate();
                    }
                }
            }
        }
    }

    public void updateGUI(int index)
    {
        parentNodes=new HorizontalFieldManager(USE_ALL_WIDTH);
        bitmap_field[index]=new BitmapField(bitmap);

        parentNodes.add(bitmap_field[index]);
        parent_lables[index]=new LabelField("Day"+index,Field.FOCUSABLE){
            protected boolean navigationClick(int status, int time) {
                fieldChangeNotify(1);
                return true;
            };
        };
        parentNodes.add(parent_lables[index]);
        parent_lables[index].setChangeListener(this);
        submanager[index].add(parentNodes);
    }
}

我把它拼凑在一起,可能需要你做一些调整,才能得到你想要的东西,但它应该是你可以使用的东西。假设我正确理解了你的问题

您需要创建一个基本字段、一个能够管理这些字段列表的帮助器和一个回调。您需要一个基本字段这一事实是最大的缺点,因为它会立即排除您拥有的所有其他小部件,因为它们需要使用paint方法从头开始制作。通过扩展VerticalFieldManager而不是field,可以使用VerticalFieldManager而不是field

关于java类。 My CollapsableField.java如下所示:

public abstract class CollapsableField extends Field
{
    // We make use of a different listener than the FieldChangeListener, since you can only attach one and we will most likely want to do so, we can't "occupy" the listener.
    private CollapseListener listener;
    private boolean collapsed;
    protected int collapsedWidth;
    protected int collapsedHeight;

    public CollapsableField()
    {
        collapsed = true;

        // Field is completely collapsed by default
        collapsedWidth = 0;
        collapsedHeight = 0;
    }

    public void setCollapseListener(CollapseListener listener)
    {
        this.listener = listener;
    }

    public final boolean isCollapsed()
    {
        return collapsed;
    }

    public final void collapse()
    {
        this.collapsed = true;
        if (listener != null)
        {
            listener.onCollapse(this);
        }
        fieldChangeNotify(0); // Notify that the field has changed, so that attached field change listeners will fire
        updateLayout(); // re-call layout
    }

    public final void expand()
    {
        this.collapsed = false;
        if (listener != null)
        {
            listener.onExpand(this);
        }
        fieldChangeNotify(0); // Notify that the field has changed, so that attached field change listeners will fire
        updateLayout(); // re-call layout
    }

    protected void layout(int width, int height)
    {
        if (collapsed)
        {
            // Set dimensions to collapsed
            setExtent(collapsedWidth, collapsedHeight);
        }
        else
        {
            // Set dimensions to what the extending class specified
            setExtent(width, height);
        }
    }

    protected final void paint(Graphics graphics)
    {
        if (collapsed)
        {
            paintCollapsed(graphics);
        }
        else
        {
            paintExpanded(graphics);
        }
    }

    protected abstract void paintCollapsed(Graphics graphics);

    protected abstract void paintExpanded(Graphics graphics);
}
然后,该组获取这些字段的列表,并在每个字段添加时应用一个侦听器。当一个场发出信号表明它已扩展时,该组将通知所有其他场自行崩溃。 CollapsableGroup.java:

public class CollapsableGroup
{
    private Vector fields;
    private CollapseListener listener;

    public CollapsableGroup()
    {
        fields = new Vector();

        listener = new CollapseListener()
        {
            public void onExpand(CollapsableField source)
            {
                for (int i = 0; i < fields.size(); i++)
                {
                    CollapsableField field = (CollapsableField) fields.elementAt(i);
                    if ((!field.isCollapsed()) && (field != source))
                    {
                        field.collapse();
                    }
                }
            }

            public void onCollapse(CollapsableField source)
            {
                // Don't need to handle this. Method is here just for completeness
            }
        };
    }

    public void add(CollapsableField field)
    {
        field.setCollapseListener(listener);
        fields.addElement(field);
    }

    public void remove(CollapsableField field)
    {
        field.setCollapseListener(null);
        fields.removeElement(field);
    }

    /**
     * Returns the currently expanded field. Returns <b>null</b> if all fields are collapsed.
     * 
     * @return
     */
    public CollapsableField getExpandedField()
    {
        for (int i = 0; i < fields.size(); i++)
        {
            CollapsableField field = (CollapsableField) fields.elementAt(i);
            if (!field.isCollapsed())
            {
                return field;
            }
        }

        return null;
    }

    public void expand(CollapsableField field)
    {
        field.expand(); // Listeners should take care of the rest
    }

    public void collapseAll()
    {
        for (int i = 0; i < fields.size(); i++)
        {
            CollapsableField field = (CollapsableField) fields.elementAt(i);
            if (!field.isCollapsed())
            {
                field.collapse();
            }
        }
    }
}
这三门课应该是你所需要的。下面的类是我的示例/测试类。 TestLabel.java是扩展类的一个示例:

public class TestLabel extends CollapsableField
{
    private String text;
    private String collapsedText;

    public TestLabel(String text, String collapsedText)
    {
        this.text = text;
        this.collapsedText = collapsedText;

        // Tells the layout method to collapse to the size of this text
        collapsedWidth = getFont().getAdvance(collapsedText);
        collapsedHeight = getFont().getHeight();
    }

    public int getPreferredWidth()
    {
        return getFont().getAdvance(text);
    }

    public int getPreferredHeight()
    {
        return getFont().getHeight();
    }

    protected void layout(int width, int height)
    {
        super.layout(getPreferredWidth(), getPreferredHeight());
    }

    protected void paintCollapsed(Graphics graphics)
    {
        // Paints only the collapsedText
        graphics.drawText(collapsedText, 0, 0);
    }

    protected void paintExpanded(Graphics graphics)
    {
        // Paints the full Text
        graphics.drawText(text, 0, 0);
    }

    protected boolean touchEvent(TouchEvent message)
    {
        // Toggle on mouse press
        if (message.getEvent() == TouchEvent.CLICK)
        {
            if (isCollapsed())
            {
                expand();
            }
            else
            {
                collapse();
            }

            return true;
        }
        return super.touchEvent(message);
    }
}
下面的屏幕包含一些字段,以显示小部件本身和组都可以操作这些字段。 MyScreen.java:

public final class MyScreen extends MainScreen
{
    public MyScreen()
    {
        // Set the displayed title of the screen
        setTitle("MyTitle");

        final CollapsableGroup group = new CollapsableGroup();

        final TestLabel label1 = new TestLabel("Label1", "L1");
        label1.setBackground(BackgroundFactory.createSolidBackground(0x999999));
        group.add(label1);

        final TestLabel label2 = new TestLabel("Label2", "L2");
        label2.setBackground(BackgroundFactory.createSolidBackground(0xBBBBBB));
        group.add(label2);

        final TestLabel label3 = new TestLabel("Label3", "L3");
        label3.setBackground(BackgroundFactory.createSolidBackground(0xDDDDDD));
        group.add(label3);

        ButtonField collapseAll = new ButtonField("Collapse All")
        {
            protected boolean navigationClick(int status, int time)
            {
                group.collapseAll();
                return true;
            }
        };
        add(collapseAll);

        ButtonField expand1 = new ButtonField("Expand1")
        {
            protected boolean navigationClick(int status, int time)
            {
                group.expand(label1);
                return true;
            }
        };
        add(expand1);

        ButtonField expand2 = new ButtonField("Expand2")
        {
            protected boolean navigationClick(int status, int time)
            {
                group.expand(label2);
                return true;
            }
        };
        add(expand2);

        ButtonField expand3 = new ButtonField("Expand3")
        {
            protected boolean navigationClick(int status, int time)
            {
                group.expand(label3);
                return true;
            }
        };
        add(expand3);

        add(label1);
        add(label2);
        add(label3);
    }
}

我能拿到你的skypeid吗?我不想向公众发布这样的东西。如果你愿意,我们可以在上面创建聊天室。
public final class MyScreen extends MainScreen
{
    public MyScreen()
    {
        // Set the displayed title of the screen
        setTitle("MyTitle");

        final CollapsableGroup group = new CollapsableGroup();

        final TestLabel label1 = new TestLabel("Label1", "L1");
        label1.setBackground(BackgroundFactory.createSolidBackground(0x999999));
        group.add(label1);

        final TestLabel label2 = new TestLabel("Label2", "L2");
        label2.setBackground(BackgroundFactory.createSolidBackground(0xBBBBBB));
        group.add(label2);

        final TestLabel label3 = new TestLabel("Label3", "L3");
        label3.setBackground(BackgroundFactory.createSolidBackground(0xDDDDDD));
        group.add(label3);

        ButtonField collapseAll = new ButtonField("Collapse All")
        {
            protected boolean navigationClick(int status, int time)
            {
                group.collapseAll();
                return true;
            }
        };
        add(collapseAll);

        ButtonField expand1 = new ButtonField("Expand1")
        {
            protected boolean navigationClick(int status, int time)
            {
                group.expand(label1);
                return true;
            }
        };
        add(expand1);

        ButtonField expand2 = new ButtonField("Expand2")
        {
            protected boolean navigationClick(int status, int time)
            {
                group.expand(label2);
                return true;
            }
        };
        add(expand2);

        ButtonField expand3 = new ButtonField("Expand3")
        {
            protected boolean navigationClick(int status, int time)
            {
                group.expand(label3);
                return true;
            }
        };
        add(expand3);

        add(label1);
        add(label2);
        add(label3);
    }
}