2014-03-28 28 views
4

我正在使用自定義GridField管理器以2列顯示具有指定寬度的詳細信息。 當右側的值太大時,值不包裝到下一行。使用自定義GridField管理器在BB中無法使用自動換行功能

這是我的Custom GridField Manager類。

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

如果任何1有想法,然後請幫助。

+0

您能否確認,在給出的示例中,它是不包裝的value_CustEmail LabelField。你可以讓它聚焦,看看你是否可以通過水平滾動查看全部內容? –

+0

通過使其變得可以聚焦,它變得可以聚焦但不可滾動。 –

+0

我使用了LabelField.FOCUSABLE。現在你能告訴我如何使它水平滾動? –

回答

3

您將寬度分爲兩部分..但我的建議是您應該將寬度分爲三部分。像

int[] width = { (int) (Display.getWidth()/3.2), 
        (int) (Display.getWidth()/32),(int) (Display.getWidth()/1.6) }; 

和分化「:」新標籤字段。

這裏是我建議的代碼..我已經嘗試過,它工作正常..

int[] width = { (int) (Display.getWidth()/3.2), 
        (int) (Display.getWidth()/32),(int) (Display.getWidth()/1.6) }; 

      final CustomGridFieldManager gfm_transactioninfo = new CustomGridFieldManager(
        width,45, 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, 10, 0, 10);// set top and left margin 

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

      value_CustEmail = new LabelField(proTransaction_details.getFromEmail()); 
      value_CustEmail.setFont(label_font); 

gfm_transactioninfo.add(lbl_CustEmail); 
         gfm_transactioninfo.add(new LabelField(" : ")); 
         gfm_transactioninfo.add(value_CustEmail); 

和你CustomGridFieldManager類是完美的..

就振作起來.. :)

+1

非常感謝..最後它爲我完成。再一次感謝你。 –

相關問題