【问题标题】:How to scroll Horizontally in labelField in Blackberry如何在黑莓的labelField中水平滚动
【发布时间】:2014-03-29 11:27:28
【问题描述】:

我想在标签字段中水平滚动。

我在自定义 GridField 管理器中添加了这个 LabelField。这是自定义GridField管理器的代码。

public class CustomGridFieldManager extends Manager {
private int[] columnWidths;
private int columns;
private int allRowHeight = -1;


public CustomGridFieldManager(int columns, long style) {
    super(style);
    this.columns = columns;
}


public CustomGridFieldManager(int[] columnWidths, long style) {
    super(style);
    this.columnWidths = columnWidths;
    this.columns = columnWidths.length;

}

public CustomGridFieldManager(int[] columnWidths, int rowHeight, long style) {
    this(columnWidths, style);
    this.allRowHeight  = rowHeight;
}

protected boolean navigationMovement(int dx, int dy, int status, int time) {

    int focusIndex = getFieldWithFocusIndex();
    while(dy > 0) {
        focusIndex += columns;
        if (focusIndex >= getFieldCount()) {
            return false; // Focus moves out of this manager
        }
        else {
            Field f = getField(focusIndex);
            if (f.isFocusable()) { // Only move the focus onto focusable fields
                f.setFocus();
                dy--;
            }
        }
    }
    while(dy < 0) {
        focusIndex -= columns;
        if (focusIndex < 0) {
            return false;
        }
        else {
            Field f = getField(focusIndex);
            if (f.isFocusable()) {
                f.setFocus();
                dy++;
            }
        }
    }

    while(dx > 0) {
        focusIndex ++;
        if (focusIndex >= getFieldCount()) {
            return false;
        }
        else {
            Field f = getField(focusIndex);
            if (f.isFocusable()) {
                f.setFocus();
                dx--;
            }
        }
    }
    while(dx < 0) {
        focusIndex --;
        if (focusIndex < 0) {
            return false;
        }
        else {
            Field f = getField(focusIndex);
            if (f.isFocusable()) {
                f.setFocus();
                dx++;
            }
        }
    }
    return true;
}

protected void sublayout(int width, int height) {
    int y = 0;
    if (columnWidths == null) {
        columnWidths = new int[columns];
        for(int i = 0; i < columns; i++) {
            columnWidths[i] = width/columns;
        }
    }
    Field[] fields = new Field[columnWidths.length];
    int currentColumn = 0;
    int rowHeight = 0;
    for(int i = 0; i < getFieldCount(); i++) {
        fields[currentColumn] = getField(i);
        layoutChild(fields[currentColumn], columnWidths[currentColumn], height-y);
        if (fields[currentColumn].getHeight() > rowHeight) {
            rowHeight = fields[currentColumn].getHeight();
        }
        currentColumn++;
        if (currentColumn == columnWidths.length || i == getFieldCount()-1) {
            int x = 0;
            if (this.allRowHeight >= 0) {
                rowHeight = this.allRowHeight;
            }
            for(int c = 0; c < currentColumn; c++) {
                long fieldStyle = fields[c].getStyle();
                int fieldXOffset = 0;
                long fieldHalign = fieldStyle & Field.FIELD_HALIGN_MASK;
                if (fieldHalign == Field.FIELD_RIGHT) {
                    fieldXOffset = columnWidths[c] - fields[c].getWidth();
                }
                else if (fieldHalign == Field.FIELD_HCENTER) {
                    fieldXOffset = (columnWidths[c]-fields[c].getWidth())/2;
                }

                int fieldYOffset = 0;
                long fieldValign = fieldStyle & Field.FIELD_VALIGN_MASK;
                if (fieldValign == Field.FIELD_BOTTOM) {
                    fieldYOffset = rowHeight - fields[c].getHeight();
                }
                else if (fieldValign == Field.FIELD_VCENTER) {
                    fieldYOffset = (rowHeight-fields[c].getHeight())/2;
                }

                setPositionChild(fields[c], x+fieldXOffset, y + fieldYOffset);
                x += columnWidths[c];
            }
            currentColumn = 0;
            y += rowHeight;
        }
        if (y >= height) {
            break;
        }
    }
    int totalWidth = 0;
    for(int i = 0; i < columnWidths.length; i++) {
        totalWidth += columnWidths[i];
    }
    setExtent(totalWidth, Math.min(y, height));
}

}

在另一个类中,我使用了这个自定义的 GridField 管理器类。

int[] width = { (int) (Display.getWidth() / 2.9),
                (int) (Display.getWidth() / 1.1) };

      final CustomGridFieldManager gfm_transactioninfo = new CustomGridFieldManager(
                width, 35, Manager.VERTICAL_SCROLL | Manager.FIELD_HCENTER
                        | FOCUSABLE) {
            protected void paint(Graphics graphics) {
                // TODO Auto-generated method stub
                graphics.setColor(AppData.color_black);
                super.paint(graphics);
            }

        };
        gfm_transactioninfo.setMargin(10, 0, 0, 10);// set top and left margin

我这样添加Labelfiled,

lbl_CustEmail = new LabelField("Customer Email", LabelField.FOCUSABLE);
        lbl_CustEmail.setFont(label_font);

        value_CustEmail = new LabelField(": " +trandtail[0].getFromEmail());
        value_CustEmail.setFont(label_font);

   gfm_transactioninfo.add(lbl_CustEmail);
   gfm_transactioninfo.add(value_CustEmail);

如果有人对如何水平滚动有任何想法,请帮助我。提前致谢。

【问题讨论】:

  • 当您覆盖 navigationMovement 时,您希望触控板用户如何滚动?
  • 是否有特定原因不能简单地将标签放在设置为水平滚动的 FieldManager 中?即gfm_transactioninfo.add(manager);
  • @Kevin - 这种方法的一个问题是他已经覆盖了 GFM 中的 navigationMovement,那么他如何在非触摸屏设备上滚动该 Manager 中的字段?

标签: blackberry horizontal-scrolling labelfield


【解决方案1】:

通过自定义网格视图,您可以在标签字段前后添加一个 FocusableNullField。通过这样做,一旦焦点位于第一个 null 字段上,您就可以水平滚动到下一个 focusablenullfield 并显式使 labelfield 可滚动。

【讨论】:

  • 不要认为这会起作用,我认为添加 NullFields 只是意味着您可以专注于开始和结束,而不是可滚动的。您可以使 LabelField 可聚焦以在开始时实现与 NullField 类似的效果,甚至可以将 LabelField 替换为 RichTextField,它维护一个“插入符号”,因此在这方面是可滚动的。有许多类似的选项,但对我来说,问题是它们都不能在带有提供的 navigationMeovement 覆盖的触控板设备上工作。因此,我的问题 OP 没有回答。
猜你喜欢
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
  • 2012-02-04
相关资源
最近更新 更多