在焦点(突出显示)颜色上设置BlackBerry图像字段

时间:2013-05-14 03:06:22

标签: blackberry blackberry-simulator blackberry-eclipse-plugin blackberry-jde

我正在开发一个BlackBerry应用程序,它使用表行管理器来创建表视图。在我的应用程序中,我将位图字段添加为行背景,然后字段突出显示的默认蓝色消失了。我在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);
            } 

        };

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

示例行背景: enter image description here

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

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;
    }
}

有没有人知道这里发生了什么?我想让它突出显示默认的蓝色。我是黑莓手机的新手。

1 个答案:

答案 0 :(得分:1)

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

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);
}

结果

enter image description here

注意:每行的右侧都是隐藏的,因为我在360px宽屏幕上测试了这一点,并且您的代码硬编码字段位置。所以,他们离开了屏幕的右侧。但这与焦点突出显示问题无关。