2009-09-15 92 views
2

我想在屏幕的下半部分創建一個RichTextField,同時在屏幕的上半部分繪製自己的自定義圖形。黑莓手機有可能嗎?它試圖定義一個LayoutManager並嘗試將RichTextField放置在屏幕的底部,但RichTextField則在整個屏幕上滾動。這是LayoutManager()的代碼。這是正確的方式,還是有任何其他方式來做我上面提到的。在BlackBerry中創建自定義佈局

class LayoutManager extends Manager 
{ 

    public LayoutManager() 
    { 
    //construct a manager with vertical scrolling  
    super(VERTICAL_SCROLL); 
    } 

    //overwrite the nextFocus method for custom navigation 
    protected int nextFocus(int direction, boolean alt) 
    { 
     return super.nextFocus(direction, alt); 
    } 

    protected void sublayout(int width, int height) 
    { 
    Field field; 
    //get total number of fields within this manager 
    int numberOfFields = getFieldCount();  
    int x = 0; 
    int y = 0; 
    System.out.println("******** Fields: " + numberOfFields + " W/H: " + width + "/" + height); 
    for(int i = 0;i < numberOfFields;i++) { 
     field = getField(i);  //get the field 
     x = 20; 
     y = 80; 
     System.out.println("******** X/Y: " + x + "/" + y); 
     setPositionChild(field, x, y); //set the position for the field 
     layoutChild(field, width, y); //lay out the field 
    } 
    setPosition(0, 80); 
    setExtent(width, 80); 

    } 

    public int getPreferredWidth() 
    { 
    return 160; 
    } 

    public int getPreferredHeight() 
    { 
    int height= 0; 
    int numberOfFields= getFieldCount(); 

    for(int i= 0; i < numberOfFields; i++) 
    { 
     height += getField(i).getPreferredHeight(); 
    } 
    return 160; 
    } 
} 

回答

4

UPDATE - 自定義滾動條

custom scrollbar http://img146.imageshack.us/img146/7775/scroll.png

VerticalFieldManager自定義大小限制和滾動:

class SizedVFM extends VerticalFieldManager { 
    int mWidth; 
    int mHeight; 

    public SizedVFM(int width, int height) { 
     super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR); 
     mWidth = width; 
     mHeight = height; 
    } 

    public int getPreferredHeight() { 
     return mHeight; 
    } 

    public int getPreferredWidth() { 
     return mWidth; 
    } 

    protected void sublayout(int maxWidth, int maxHeight) { 
     super.sublayout(maxWidth, maxHeight); 
     setExtent(getPreferredWidth(), getPreferredHeight()); 
    } 

    protected void paint(Graphics graphics) { 
     super.paint(graphics); 
     if (getVisibleHeight() < getVirtualHeight()) { 
      int y1 = 0, y2 = 0, x1 = 0, x2 = 0; 
      int scrollOff = getVerticalScroll(); 
      if (scrollOff > 0) { 
       y1 = scrollOff + 12; 
       y2 = scrollOff + 2; 
       x1 = getVisibleWidth() - 20; 
       x2 = getVisibleWidth() - 2; 

       graphics.setColor(Color.DARKRED); 
       int[] xPts = new int[] { x1, x2, x1 + 9 }; 
       int[] yPts = new int[] { y1, y1, y2 }; 
       graphics.drawFilledPath(xPts, yPts, null, null); 
      } 
      if (scrollOff < (getVirtualHeight() - getVisibleHeight())) { 
       y1 = scrollOff + getVisibleHeight() - 12; 
       y2 = scrollOff + getVisibleHeight() - 2; 
       x1 = getVisibleWidth() - 20; 
       x2 = getVisibleWidth() - 2; 
       graphics.setColor(Color.DARKRED); 
       int[] xPts = new int[] { x1, x2, x1 + 9 }; 
       int[] yPts = new int[] { y1, y1, y2 }; 
       graphics.drawFilledPath(xPts, yPts, null, null); 
      } 
     } 
    } 
} 

場繪畫和文字:

class HeaderPainting extends SizedVFM { 
    BitmapField mBitmapField; 
    public HeaderPainting(Bitmap bitmap, int width, int height) { 
     super(width, height); 
     add(mBitmapField = new BitmapField(bitmap, FOCUSABLE)); 
    } 
} 
class FooterText extends SizedVFM { 
    ExRichTextField mTextField; 
    public FooterText(String text, int width, int height) { 
     super(width, height); 
     int bgColor = Color.SANDYBROWN; 
     int textColor = Color.DARKRED; 
     add(mTextField = new ExRichTextField(text, bgColor, textColor)); 
    } 
    class ExRichTextField extends RichTextField { 
     int mTextColor; 
     int mBgColor; 
     public ExRichTextField(String text, int bgColor, int textColor) { 
      super(text); 
      mTextColor = textColor; 
      mBgColor = bgColor; 
     } 
     protected void paint(Graphics graphics) { 
      graphics.clear(); 
      graphics.setColor(mBgColor); 
      graphics.fillRect(0, 0, getWidth(), getHeight()); 
      graphics.setColor(mTextColor); 
      super.paint(graphics); 
     } 
    } 
} 

示例使用:

class Scr extends MainScreen { 
    HeaderPainting mBitmapField; 
    FooterText mTextField; 
    public Scr() { 
     int width = Display.getWidth(); 
     int height = Display.getHeight()/2; 
     Bitmap bitmap = customPaint(width, height); 
     String text = "Lorem ipsum dolor sit amet, consectetuer " 
       + "adipiscing elit, sed diam nonummy nibh euismod " 
       + "tincidunt ut laoreet dolore magna aliquam erat " 
       + "volutpat. Ut wisi enim ad minim veniam, quis " 
       + "nostrud exerci tation ullamcorper suscipit " 
       + "lobortis nisl ut aliquip ex ea commodo consequat. " 
       + "Duis autem vel eum iriure dolor in hendrerit in " 
       + "vulputate velit esse molestie consequat, vel " 
       + "illum dolore eu feugiat nulla facilisis at vero " 
       + "eros et accumsan et iusto odio dignissim qui " 
       + "blandit praesent luptatum zzril delenit augue " 
       + "duis dolore te feugait nulla facilisi."; 
     add(mBitmapField = new HeaderPainting(bitmap, width, height)); 
     add(mTextField = new FooterText(text, width, height)); 
    } 
    protected Bitmap customPaint(int width, int height) { 
     Bitmap bmp = new Bitmap(width, height); 
     Graphics graphics = new Graphics(bmp); 
     graphics.setColor(Color.BLUE); 
     graphics.fillRect(10, 10, width - 20, height - 20); 
     graphics.setColor(Color.RED); 
     graphics.fillRect(10, 10, 50, height - 20); 
     return bmp; 
    } 
} 

如果你不喜歡裏面的重點看RichTextField
Blackberry Java: TextField without the caret?

+0

感謝coldice,這就是我想要的 – Ram 2009-09-15 19:32:12

+0

不客氣! – 2009-09-15 20:03:45

+0

還有一件事,有沒有一種方法可以爲RichTextField設置背景和字體顏色? – Ram 2009-09-15 20:32:58

0

您可以從您的RichTextField中取消設置焦點。並且請記住,您可以使用空字段來控制焦點。 我最近有與自定義列表字段滾動的戰鬥。您可以查看我的post,這可能會有所幫助。

+0

如果你在關注RichTextField格外有興趣,你可以從它派生和辭退DRAWFOCUS的默認實現方法。 – nixau 2009-09-15 10:57:51

+0

在沒有RichTextField在屏幕上移動位置的情況下,RichTextField的內容是否會在其內滾動? – Ram 2009-09-15 12:41:45

+0

它不應該除非它重新聚焦。 – nixau 2009-09-15 14:07:42