【问题标题】:Expanding one field while others are collapsed in blackberry java在黑莓java中扩展一个字段而其他字段折叠
【发布时间】:2015-03-24 09:31:57
【问题描述】:

我正在开发一个应用程序,在该应用程序中,我根据将标志设置为 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);
    }
}

【问题讨论】:

    标签: java eclipse blackberry


    【解决方案1】:

    我将它拼凑在一起,可能需要您进行一些调整才能完全按照您的意愿进行操作,但它应该是您可以使用的东西。假设我正确理解了您的问题。

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

    关于 java 类。 我的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);
    }
    

    Group 然后获取这些列表,在添加的每个字段中应用一个侦听器。当一个字段表示它已展开时,该组将告诉所有其他字段自行折叠。 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();
                }
            }
        }
    }
    

    最后是监听器接口。 CollapseListener.java:

    interface CollapseListener
    {
        public void onExpand(CollapsableField source);
    
        public void onCollapse(CollapsableField source);
    }
    

    这三个类应该是你所需要的。下面的类是我的示例/测试类。 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。
    • 我不想向公众发布类似的内容。如果您愿意,我们可以在 SO 上创建一个聊天室。
    猜你喜欢
    • 1970-01-01
    • 2010-10-02
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多