2009-08-27 151 views
1

我試圖把一個對話框應該像這樣的:黑莓 - 自定義大小EditField中

填寫以下領域
_______________喜歡____________________

其中「_」行是EditFields。

我將所有字段粘貼到Horizo​​ntalFieldManager中,我將其添加到對話框中。不幸的是,第一個EditField消耗了第一行的所有空間。我試圖通過創建自己的擴展BasicEditField的類來覆蓋EditField的getPreferredWidth()方法,但沒有成功。

當然,必須有一個簡單的方法來強制編輯字段的一定大小。我錯過了什麼?

回答

5

就像DaveJohnston說:

class LikesHFManager extends HorizontalFieldManager { 
    EditField mEditFieldLeft; 
    LabelField mLabelField; 
    EditField mEditFieldRight; 
    String STR_LIKES = "likes"; 
    int mLabelWidth = 0; 
    int mEditWidth = 0; 
    int mOffset = 4; 

    public LikesHFManager() { 
     mEditFieldLeft = new EditField(); 
     mLabelField = new LabelField(STR_LIKES); 
     mEditFieldRight = new EditField(); 

     mLabelWidth = mLabelField.getFont().getAdvance(STR_LIKES); 
     int screenWidth = Display.getWidth(); 
     mEditWidth = (screenWidth - mLabelWidth) >> 1; 
     mEditWidth -= 2 * mOffset; 

     // calculate max with of one character 
     int chMaxWith = mEditFieldLeft.getFont().getAdvance("W"); 
     // calculate max count of characters in edit field 
     int chMaxCnt = mEditWidth/chMaxWith; 

     mEditFieldLeft.setMaxSize(chMaxCnt); 
     mEditFieldRight.setMaxSize(chMaxCnt); 

     add(mEditFieldLeft); 
     add(mLabelField); 
     add(mEditFieldRight); 
    } 

    protected void sublayout(int maxWidth, int maxHeight) { 

     int x = 0; 
     int y = 0; 

     int editHeight = mEditFieldLeft.getPreferredHeight(); 
     int labelHeight = mLabelField.getPreferredHeight(); 

     setPositionChild(mEditFieldLeft, x, y); 
     layoutChild(mEditFieldLeft, mEditWidth, editHeight); 
     x += mEditWidth; 
     x += mOffset; 

     setPositionChild(mLabelField, x, y); 
     layoutChild(mLabelField, mLabelWidth, labelHeight); 
     x += mLabelWidth; 
     x += mOffset; 

     setPositionChild(mEditFieldRight, x, y); 
     layoutChild(mEditFieldRight, mEditWidth, editHeight); 
     x += mEditWidth; 

     setExtent(x, Math.max(labelHeight, editHeight)); 
    } 
} 
+0

謝謝!這個例子肯定會讓事情更清楚。 – Eric 2009-09-08 13:15:00

+0

不客氣! – 2009-09-10 11:29:46

4

嘗試繼承Horizo​​ntalFieldManager並重寫sublayout方法:

protected void sublayout(int maxWidth, int maxHeight) { } 

在該方法中應該調用setPositionChild()和layoutChild()爲要添加這樣可以控制每個的定位和大小的每個組件。

你也應該覆蓋各部件的佈局方法,並調用

setExtent(getPreferredWidth(), getPreferredHeight()); 

這會讓你的使用實現你已經寫了getPreferred ...方法。

希望這會有所幫助。

0

大廈最大Gontar的解決方案,這應該解決再轉Horizo​​ntalFieldManagers的字段分配寬度的一般問題:

import net.rim.device.api.ui.container.*; 
import net.rim.device.api.ui.*; 

public class FieldRowManager extends HorizontalFieldManager { 
    public FieldRowManager(final long style) 
    { 
     super(style); 
    } 
    public FieldRowManager() 
    { 
     this(0); 
    } 

    private SubField FirstSubField = null; 
    private SubField LastSubField = null; 
    private static class SubField 
    { 
     public final Field Field; 
     public final int Width; 
     public final int Offset; 
     private SubField Next; 
     public SubField(final FieldRowManager container, final Field field, final int width, final int offset) 
     { 
      Field = field; 
      Width = width; 
      Offset = offset; 

      if (container.LastSubField == null) 
      { 
       container.FirstSubField = this; 
      } 
      else 
      { 
       container.LastSubField.Next = this; 
      } 
      container.LastSubField = this; 
     } 
     public SubField getNext() 
     { 
      return Next; 
     } 
    } 

    public void add(final Field field) 
    { 
     add(field, field.getPreferredWidth()); 
    } 
    public void add(final Field field, final int width) 
    { 
     add(field, width, 0); 
    } 
    public void add(final Field field, final int width, final int offset) 
    { 
     new SubField(this, field, width, offset); 
     super.add(field); 
    } 

    protected void sublayout(final int maxWidth, final int maxHeight) 
    { 
     int x = 0; 
     int height = 0; 
     SubField subField = FirstSubField; 
     while (subField != null) 
     { 
      final Field field = subField.Field; 
      final int fieldHeight = field.getPreferredHeight(); 
      this.setPositionChild(field, x, 0); 
      this.layoutChild(field, subField.Width, fieldHeight); 
      x += subField.Width+subField.Offset; 
      if (fieldHeight > height) 
      { 
       height = fieldHeight; 
      } 

      subField = subField.getNext(); 
     } 
     this.setExtent(x, height); 
    } 
} 

只需調用的重載添加方法來指定寬度,以及下一個字段之前的偏移量空間。雖然這不允許刪除/替換字段。

RIM在標準庫中不提供此功能令人厭煩。 Horizo​​ntalFieldManager 只是這樣工作。