0

我正在开发一个使用表格行管理器来创建表格视图的黑莓应用程序。在我的应用程序中,我添加了位图字段作为行背景,然后字段突出显示的默认蓝色消失了。我在 stackoverflow 中阅读了一些答案并实现了此代码以尝试使其以默认颜色突出显示:

        //Adds background image
        final Bitmap bck; 
        bck = Bitmap.getBitmapResource("inner-back-stripe.jpg");
        BitmapField backgrnd = new BitmapField(bck,BitmapField.FOCUSABLE){
            protected void drawFocus(Graphics graphics, boolean on) 
            {
              setSpace(5, 5);
              super.drawFocus(graphics, on);
            } 

        };

但仍然有同样的问题,行没有突出显示,滚动时只有文本变为白色。

示例行背景: 在此处输入图像描述

在这里,我粘贴了我使用过的整个代码

public class LeaveDetails extends ListField implements ListFieldCallback  {

    TableRowManager row;
    Vector rows;
    private String[] arrdNames = {"Udith","Mohammadu","Rajaram","David"};
    private String[] arrDate = {"12-Jan-2013","17-Feb-2013","20-Mar-2013","14-May-2013"};
    private String[] arrType = {"Medical Leave","Annual Leave","Medical Leave","Annual Leave"};
    private int[] arrCount = {3,5,2,6};

    public LeaveDetails(){
        super(0, ListField.MULTI_SELECT);
        setRowHeight(100);
        setEmptyString("", DrawStyle.HCENTER);
        setCallback(this);
        rows = new Vector();

        for (int x = 0; x < 4; x++) {
            row = new TableRowManager();
            //Adds background image
            final Bitmap bck;
            bck = Bitmap.getBitmapResource("inner-back-stripe.jpg");
            BitmapField backgrnd = new BitmapField(bck,BitmapField.FOCUSABLE){
                protected void drawFocus(Graphics graphics, boolean on)
                {
                    setSpace(5, 5);
                    super.drawFocus(graphics, on);
                }
            };
            row.add(backgrnd);

            //BitmapField backgrnd = new BitmapField(Bitmap.getBitmapResource("inner-back-stripe.jpg"),Field.FOCUSABLE);
            //row.add(backgrnd); //Field 0

            //Adds names
            LabelField lfName = new LabelField(String.valueOf(arrdNames[x]),FIELD_HCENTER);
            lfName.setFont(lfName.getFont().derive(Font.BOLD,30));
            row.add(lfName);//Field 1

            //Adds date
            LabelField date = new LabelField("Applied on : "+arrDate[x]);
            row.add(date);//Field 2

            //Adds type
            LabelField type = new LabelField("Leave type : "+arrType[x]);
            row.add(type);//Field 3

            //Adds duration in numbers
            LabelField lfCountNotifications = new LabelField(String.valueOf(arrCount[x]),FIELD_HCENTER);
            lfCountNotifications.setFont(lfCountNotifications.getFont().derive(Font.BOLD,72));
            row.add(lfCountNotifications);//Field 4

            //Adds word "Duration"
            LabelField lfNewNotification = new LabelField("Duration/Days",Field.FIELD_HCENTER);
            lfNewNotification.setFont(lfNewNotification.getFont().derive(Font.GEORGIAN_SCRIPT,30));
            row.add(lfNewNotification);//Field 5

            //Adds right arrow image
            BitmapField showMore = new BitmapField(Bitmap.getBitmapResource("more.png"));
            row.add(showMore);//Field 6

            //Adds rows to the table
            rows.addElement(row);
        }//End of the for loop
        setSize(rows.size());
    }

    // ListFieldCallback Implementation
    public void drawListRow(ListField listField, Graphics g, int index, int y,int width) {
        LeaveDetails list = (LeaveDetails) listField;
        TableRowManager rowManager = (TableRowManager) list.rows.elementAt(index);
        rowManager.drawRow(g, 0, y, width, list.getRowHeight());
    }

    //Tracks field click event
    protected boolean trackwheelClick(int status, int time)
    {
        int index = getSelectedIndex();
        switch (index) {
            case 0: Dialog.inform(index+" Clicked");
            break;
            case 1: Dialog.inform(index+" Clicked");
            break;
            case 2: Dialog.inform(index+" Clicked");
            break;
            case 3: Dialog.inform(index+" Clicked");
            break;
            default: Dialog.inform("Error!");
            break;
        }
        return true;
    }

    private class TableRowManager extends Manager {
        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) {
            // Arrange the cell fields within this row manager.
            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);
            // Restore the graphics context.
            g.popContext();
        }

        // Arrages this manager's controlled fields from left to right within
        // the enclosing table's columns.
        protected void sublayout(int width, int height) {
            // set the size and position of each field.
            int fontHeight = Font.getDefault().getHeight();
            int preferredWidth = getPreferredWidth();

            //Field background image
            Field field = getField(0);
            layoutChild(field, 640, 166);
            setPositionChild(field, 0, 0);

            // Name
            field = getField(1);
            layoutChild(field, 640, fontHeight+4);
            setPositionChild(field, 0, 5);

            // Date
            field = getField(2);
            layoutChild(field, 500, fontHeight+4);
            setPositionChild(field, 0, 30);

            //Type
            field = getField(3);
            layoutChild(field, 500, fontHeight+1);
            setPositionChild(field, 0, 60/*fontHeight+6*/);

            //Duration number
            field = getField(4);
            layoutChild(field, 100, 100);
            setPositionChild(field, 450, 5);

            //"Duration" word
            field = getField(5);
            layoutChild(field, 300, 100);
            setPositionChild(field, 400, 62);

            //More image
            field = getField(6);
            layoutChild(field, 100, 100);
            setPositionChild(field, 575, 20);

            setExtent(preferredWidth, getPreferredHeight());
        }

        // The preferred width of a row is defined by the list renderer.
        public int getPreferredWidth() {
            return 640;
        }

        // The preferred height of a row is the "row height" as defined in the
        // enclosing list.
        public int getPreferredHeight() {
            return getRowHeight();
        }
    }

    public Object get(ListField listField, int index) {
        return null;
    }

    public int getPreferredWidth(ListField listField) {
        return 0;
    }

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

有没有人知道这里发生了什么?我想让它以默认的蓝色突出显示。我对黑莓非常陌生。

4

1 回答 1

1

我认为这里的问题与您的代码使用 aBitmapField来产生背景这一事实有关。我会建议其他几种方法来解决您的行焦点问题。

1.如果背景必须是位图

有时,这是必要的。如果您有复杂的背景设计或特殊渐变,则可能需要背景图像。为此,我仍然会删除您的BitmapField. 因此,删除此代码:

    final Bitmap bck; 
    bck = Bitmap.getBitmapResource("inner-back-stripe.jpg");
    BitmapField backgrnd = new BitmapField(bck,BitmapField.FOCUSABLE){

不要调用add(backgrnd),然后在你的sublayout()方法中,删除这个:

    Field field = getField(0);  
    layoutChild(field, 640, 166);  
    setPositionChild(field, 0, 0);  

最后,记住在sublayout()布置其他字段的方法中更改字段索引值(索引 1-6 -> 0-5)。

然后,在列表的构造函数中使用额外的一行代码创建位图背景:

   public LeaveDetails(){
      super(0, ListField.MULTI_SELECT);
      setRowHeight(100);
      setEmptyString("", DrawStyle.HCENTER);
      setCallback(this);

      setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("inner-back-stripe.jpg")));

现在,这将为整个创建一个背景ListField,而不仅仅是一行。但是,就您而言,这应该可行,因为您的背景只是纯绿色。

2. 完全避免位图

在您的情况下,我根本不推荐位图。您在应用程序中使用了额外的空间,并且可能会通过使用位图(稍微)损害性能,而您想要的只是纯色背景。所以,删除上面我告诉你要删除的代码,而不是setBackground()我在最后一个代码片段中显示的调用,使用这个:

   public LeaveDetails(){
      super(0, ListField.MULTI_SELECT);
      setRowHeight(100);
      setEmptyString("", DrawStyle.HCENTER);
      setCallback(this);

      setBackground(BackgroundFactory.createSolidBackground(0xBDCE66));

0xBDCE66看起来像您在问题中显示的绿色的十六进制颜色代码。

行分隔符

在您最初的问题中,您还在行之间显示了白色分隔条。我的前两个解决方案不会给你那个。但是,您可以通过在方法中添加几行来轻松地添加分隔符drawListRow()

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

      // draw a separator of a given color (e.g. white)
      int oldColor = g.getColor();
      g.setColor(Color.WHITE);
      int h = getRowHeight();
      //g.drawLine(0, y+h-1, width, y+h-1);   // for 1 pixel thick line
      g.fillRect(0, y+h-3, width, 3);         // for 3 pixel thick "line"
      g.setColor(oldColor);
}

结果

在此处输入图像描述

注意:每行的右侧是隐藏的,因为我在 360 像素宽的屏幕上对此进行了测试,并且您的代码对字段位置进行了硬编码。所以,它们不在屏幕的右侧。不过,这与焦点突出问题无关。

于 2013-05-14T07:26:01.907 回答