且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在黑莓手机创建自定义布局

更新时间:2023-02-26 21:57:04

更新 - 自定义滚动条

在黑莓手机创建自定义布局

VerticalFieldManager自定义大小限制和滚动:

 类SizedVFM扩展了VerticalFieldManager {
    INT mWidth;
    INT mHeight;    公共SizedVFM(INT宽度,高度INT){
    超(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
    mWidth =宽度;
    mHeight =高度;
    }    公众诠释的get preferredHeight(){
    返回mHeight;
    }    公众诠释的get preferredWidth(){
    返回mWidth;
    }    保护无效sublayout(INT了maxWidth,诠释了maxHeight){
    super.sublayout(了maxWidth,了maxHeight);
    setExtent(GET preferredWidth(),获得preferredHeight());
    }    保护无效漆(图形图像){
    super.paint(图形);
    如果(getVisibleHeight()&所述; getVirtualHeight()){
    INT Y1 = 0,Y2 = 0,X1 = 0,X2 = 0;
    INT scrollOff = getVerticalScroll();
    如果(scrollOff大于0){
    Y1 = scrollOff + 12;
    Y2 = scrollOff + 2;
    X1 = getVisibleWidth() - 20;
    X2 = getVisibleWidth() - 2;    graphics.setColor(Color.DARKRED);
    INT [] xPts =新INT [] {X1,X2,X1 + 9};
    INT [] yPts =新INT [] {Y1,Y1,Y2};
    graphics.drawFilledPath(xPts,yPts,NULL,NULL);
    }
    如果(scrollOff≤(getVirtualHeight() - getVisibleHeight())){
    Y1 = scrollOff + getVisibleHeight() - 12;
    Y2 = scrollOff + getVisibleHeight() - 2;
    X1 = getVisibleWidth() - 20;
    X2 = getVisibleWidth() - 2;
    graphics.setColor(Color.DARKRED);
    INT [] xPts =新INT [] {X1,X2,X1 + 9};
    INT [] yPts =新INT [] {Y1,Y1,Y2};
    graphics.drawFilledPath(xPts,yPts,NULL,NULL);
    }
    }
    }
}

字段绘画和文字:

 类HeaderPainting扩展SizedVFM {
    BitmapField mBitmapField;
    公共HeaderPainting(位图位图,诠释的宽度,高度INT){
    超级(宽度,高度);
    添加(mBitmapField =新BitmapField(位图,可聚焦));
    }
}
类FooterText扩展SizedVFM {
    ExRichTextField mTextField中;
    公共FooterText(字符串文字,诠释的宽度,高度INT){
    超级(宽度,高度);
    INT BGCOLOR = Color.SANDYBROWN;
    INT文字颜色= Color.DARKRED;
    添加(mTextField中=新ExRichTextField(文字,BGCOLOR,文字颜色));
    }
    类ExRichTextField扩展RichTextField {
    INT mTextColor;
    INT mBgColor;
    公共ExRichTextField(字符串文字,诠释BGCOLOR,诠释文字颜色){
    超(文本);
    mTextColor =文字颜色;
    mBgColor = BGCOLOR;
    }
    保护无效漆(图形图像){
    graphics.clear();
    graphics.setColor(mBgColor);
    graphics.fillRect(0,0,的getWidth(),的getHeight());
    graphics.setColor(mTextColor);
    super.paint(图形);
    }
    }
}

使用的示例:

 类血肌酐扩展MainScreen {
    HeaderPainting mBitmapField;
    FooterText mTextField中;
    公共SCR(){
    INT宽度= Display.getWidth();
    INT高度= Display.getHeight()/ 2;
    位图的位图= CustomPaint的(宽度,高度);
    字符串文本=Lorem存有悲坐阿梅德,consectetuer
    +adipiscing ELIT,sed的直径nonummy NIBH euismod
    +tincidunt UT laoreet dolore麦格纳aliquam ERAT
    +volutpat。UT斯达康WISI enim广告微量veniam,QUIS
    +nostrud晚会结束ullamcorper suscipit
    +lobortis nisl UT aliquip前EA commodo consequat。
    +,在DUIS autem VEL EUM iriure悲在hendrerit
    +vulputate velit ESSE molestie consequat,VEL
    +ILLUM dolore欧盟feugiat法无facilisis在维罗
    +爱神等accumsan等iusto奥迪奥dignissim魁
    +blandit praesent luptatum zzril delenit augue
    +DUIS dolore TE feugait法无facilisi。
    添加(mBitmapField =新HeaderPainting(位图,宽,高));
    添加(mTextField中=新FooterText(文字,宽,高));
    }
    保护位图CustomPaint的(INT宽度,高度INT){
    BMP位图=新位图(宽,高);
    图形图形=新的图形(BMP);
    graphics.setColor(Color.BLUE);
    graphics.fillRect(10,10,宽度 - 20,高度 - 20);
    graphics.setColor(Color.RED);
    graphics.fillRect(10,10,50,高度 - 20);
    返回BMP;
    }
}

如果你不喜欢里面的重点看RichTextField

Blackberry Java的:文本字段的没有的插入符

I want to create a RichTextField at the bottom half of the screen, while I paint my own custom graphics at the top half of the screen. Is that possible in BlackBerry? It tried defining a LayoutManager and trying to position the RichTextField at the bottom of the screen but the RichTextField, scroll through the entire screen. This is the code for the LayoutManager(). Is it the right way or is there any other way to do what I have mentioned above.

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

UPDATE - custom scrollbar

VerticalFieldManager with custom size limitation and scrolling:

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

Fields for painting and text:

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

Example of using:

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

If you don't like focus inside RichTextField see
Blackberry Java: TextField without the caret?