【问题标题】:Add buttons to a ListField in BlackBerry将按钮添加到 BlackBerry 中的 ListField
【发布时间】:2010-11-16 05:50:44
【问题描述】:

我在 BlackBerry 中使用 ListField,并希望在行中包含一个带有两个文本字段的按钮,例如:

           Button
           Text1
           Text2  

但我无法添加按钮。我找到的所有帮助都是关于添加图像的。

【问题讨论】:

  • 我想确切地知道你想用按钮做什么,行特定的操作可以很容易地完成,不需要按钮。如果列表字段行有更多操作,我会使用菜单。

标签: user-interface blackberry listfield


【解决方案1】:
【解决方案2】:

默认情况下...列表字段将焦点集中在单个行上...而不是一行上的单个字段(正如您告诉您要添加三个字段...按钮,文本字段,文本字段)。

所以我想知道您为什么要在一行中添加按钮和两个单独的文本字段...我认为如果您只想关注按钮或仅关注文本字段,这并不容易。 ...在列表字段的单行中。

顺便说一句……这里是示例代码…………你如何在列表字段的单行中创建三个字段……

只需在您的主屏幕类中调用此列表字段类的构造函数并添加它,就像.....

DetailListField _listField = new DetailListField();
add(_listField); 

DetailListField 类 -

class DetailListField extends ListField implements ListFieldCallback
{
    private Vector rows;
    private Font font;

    public DetailListField()
    {
        this(0, ListField.USE_ALL_WIDTH | DrawStyle.LEFT);
    }

    public DetailListField(int numRows, long style)
    {
        super(0, style);

        try
        {
            rows = new Vector();
            font = Font.getDefault().derive(Font.PLAIN, 7, Ui.UNITS_pt);

            setRowHeight(-2);                       
            setCallback(this);

            for (int x = 0 ; x < 5 ; x++)
            {
                TableRowManager row = new TableRowManager();

                // button, textfield, textfield
                ButtonField _btn = new ButtonField("Button", ButtonField.CONSUME_CLICK);
                _btn.setBorder(VISUAL_STATE_NORMAL, BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1),
                        new XYEdges(0x557788, 0xAA22BB, 0x557788, 0xAA22BB),
                        Border.STYLE_SOLID));
                row.add(_btn);

                BasicEditField _basicEdit1 = new BasicEditField(BasicEditField.EDITABLE | BasicEditField.FILTER_DEFAULT);
                _basicEdit1.setBorder(VISUAL_STATE_NORMAL, BorderFactory.createSimpleBorder(new XYEdges(2,2,2,2),
                        new XYEdges(0x557788, 0xAA22BB, 0x557788, 0xAA22BB),
                        Border.STYLE_SOLID));
                row.add(_basicEdit1);

                BasicEditField _basicEdit2 = new BasicEditField(BasicEditField.EDITABLE | BasicEditField.FILTER_DEFAULT);
                _basicEdit2.setBorder(VISUAL_STATE_NORMAL, BorderFactory.createSimpleBorder(new XYEdges(2,2,2,2),
                        new XYEdges(0x994422, 0xAA22BB, 0x994422, 0xAA22BB),
                        Border.STYLE_SOLID));
                row.add(_basicEdit2);


                // add id to the vector.
                rows.addElement(row); // returnData[x][0]);

                // call draw list row
                // then call constructor of manager class
            } 

            setSize(rows.size());
            invalidate();
        } catch(Exception e) {
        }
    }

    public void drawListRow(ListField list, Graphics g, int index, int y, int width)
    {
        try
        {
            DetailListField dl = (DetailListField)list;
            TableRowManager rowManager = (TableRowManager)dl.rows.elementAt(index);
            rowManager.drawRow(g, 0, y, width, list.getRowHeight());

        } catch(Exception e) {
        }
    }   

    protected boolean keyChar(char key, int status, int time)
    {   
        if (key == Characters.ENTER)
        {
            return true;
            // We've consumed the event.    
        }
        else if(key == Characters.ESCAPE)
        {
            return true;
        }             
        return super.keyChar(key, status, time);
    }

    protected boolean navigationClick(int status, int time)
    {
        try
        {
            // use below method if want to get label value from manager.
            final int index = this.getSelectedIndex();

            if(index >= 0) {
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        Dialog.alert("Selected index number : " + (index + 1));
                    }
                });
            }
        } catch (final Exception e) {
        }
        return true;   
    }

     public Object get(ListField listField, int index)
     {
         // TODO Auto-generated method stub
         return rows.elementAt(index);
     }

     public int getPreferredWidth(ListField listField)
     {
         // TODO Auto-generated method stub
         return 0;
     }

     public int indexOfList(ListField listField, String prefix, int start)
     {
         // TODO Auto-generated method stub
         return rows.indexOf(prefix, start);
     }


    /**
     *  MANAGER CLASS  
     */
    private class TableRowManager extends Manager
    {
        int _height = 0, _width = 0;
        int yPos = 0;

        public TableRowManager()
        {
            super(0);
        }

        // Causes the fields within this row manager to be layed out then
        // painted.
        public void drawRow(Graphics g, int x, int y, int width, int height)
        {
            try
            {
                _height = height;
                _width = getPreferredWidth();

                yPos = y;

                // Arrange the cell fields within this row manager.
                // set the size and position of each field.
                layout(_width, _height);

                // Place this row manager within its enclosing list.
                setPosition(x, y);

                // Apply a translating/clipping transformation to the graphics
                // context so that this row paints in the right area.

                g.pushRegion(getExtent());
                //  Paint this manager's controlled fields.
                subpaint(g);
                g.setColor(0x00CACACA);
                g.drawLine(0, 0, getPreferredWidth(), 0);

                // Restore the graphics context.
                g.popContext();
            } catch(Exception e) {
                System.out.println("Exeception : (DetailListField) 4 : " + e.toString());
            }
        }

        // Arranges this manager's controlled fields from left to right within
        // the enclosing table's columns.
        protected void sublayout(int width, int height)
        {
            try
            {

                // set the bitmap field
                Field _field0 = getField(0);
                layoutChild(_field0, (_width/3) - 30 , _height - 20);
                setPositionChild(_field0, 2, 5);

                // set the name field
                Field _field1 = getField(1);
                _field1.setFont(font);
                layoutChild(_field1, (_width/3) - 30, _field1.getPreferredHeight());
                setPositionChild(_field1, (_width/3) - 30 + 10, 5);

                Field _field2 = getField(2);
                _field2.setFont(font);              
                layoutChild(_field2, (_width/3) - 30, _field2.getPreferredHeight());
                setPositionChild(_field2, ((_width/3) - 30)*2 + 20, 5);


                setExtent(_width, _height);

            } catch(Exception e) {
                System.out.println("Exeception : (DetailListField) 5 : " + e.toString());
            }
        }
        // The preferred width of a row is defined by the list renderer.
        public int getPreferredWidth()
        {
            return (Display.getWidth());
        }
        // The preferred height of a row is the "row height" as defined in the
        // enclosing list.
        public int getPreferredHeight()
        {
            return _height;
        }       
    }
}

但我仍然不知道如何专注于单行的单个字段...

【讨论】:

    【解决方案3】:

    用法:

    ListCallBack _callBack = new ListCallBack();
    _countries.setCallback(_callBack);
    

    代码:

    private class ListCallBack implements ListFieldCallback{
    
        public void drawListRow(ListField listField, Graphics graphics,
                                int index, int y, int width) {
            for(int i = 0; i <= 23; i++) {
                graphics.drawBitmap(0, y, 48, 48, (Bitmap) MyApp._flagVector.elementAt(index), 0, 0);
            }
    
            String text = (String)MyApp._countryVector.elementAt(index);
            graphics.drawText(text, 65, y, 0, width);
        }
    
        public Object get(ListField listField, int index) {
            return MyApp._countryVector.elementAt(index);
        }
    
        public int getPreferredWidth(ListField listField) {
            return Display.getWidth();
        }
    
        public int indexOfList(ListField listField, String prefix, int start) {
            return MyApp._countryVector.indexOf(prefix, start);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多