2014-03-29 80 views
4

我想在標籤字段中水平滾動。如何在Blackberry的labelField中水平滾動

In Below image, Long Text which is E-mail ID is not scrolled.

我加入這個中的LabelField定製GridField經理。這是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); 

如果任何人有關於如何水平滾動,請您幫助我的想法。提前致謝。

+0

嘗試http://stackoverflow.com/questions/2613062/how-can-i-display-scroll-text-like-marque-in-blackberry-using-j2me – Signare

+0

你如何期待軌跡板用戶能夠滾動,當你覆蓋navigationMovement? –

+0

是否有一個特定的原因,您不能簡單地將FieldManager中的標籤設置爲水平滾動?即'gfm_transactioninfo.add(manager);' – Kevin

回答

1

通過自定義網格視圖,您可以在標籤字段前後添加一個FocusableNullField。通過這樣做,一旦焦點位於第一個空字段,您可以水平滾動到下一個可聚焦空字段,並明確地製作labelfield可滾動字段。

+1

不要以爲這會起作用,我認爲添加NullFields只是意味着您可以專注於開始和結束,而不是可滾動的。您可以使LabelField可聚焦,以便在開始時獲得與NullField類似的效果,甚至可以用一個RichTextField代替LabelField,該RichTextField保留「插入符號」,因此可以在這方面進行滾動。有許多類似的選項,但對我而言,問題是它們都不會在提供導航平均替代的觸控板設備上工作。因此我的問題是OP沒有回答。 –