Blackberry中的自定义多行文本框

Blackberry中的自定义多行文本框,blackberry,Blackberry,有谁能告诉我如何用背景色创建高度不断增加的多行文本框 谢谢您可以在API中使用EditField,并通过以下方式为其分配背景: yourEditField.setBackground(yourBackground); 公共类CustomTextBox扩展管理器 { /** *默认保证金 */ 私有最终静态整数默认值\左\边距=10; 私有最终静态整数默认值\右\边距=10; 私有最终静态整数默认值顶部边距=5; 私有最终静态整数默认值\底部\边距=5; /** *默认填充 */ 私有最终静态i

有谁能告诉我如何用背景色创建高度不断增加的多行文本框


谢谢

您可以在API中使用EditField,并通过以下方式为其分配背景:

yourEditField.setBackground(yourBackground);
公共类CustomTextBox扩展管理器
{
/**
*默认保证金
*/
私有最终静态整数默认值\左\边距=10;
私有最终静态整数默认值\右\边距=10;
私有最终静态整数默认值顶部边距=5;
私有最终静态整数默认值\底部\边距=5;
/**
*默认填充
*/
私有最终静态int默认值\左\填充=10;
私有最终静态int默认值\右\填充=10;
私有最终静态int默认值\顶部\填充=5;
私有最终静态int默认值\底部\填充=5;
/**
*文本框周围的边距
*/
private int topMargin=默认的顶部边缘;
private int bottomMargin=默认的底部边缘;
private int leftMargin=默认值\u leftMargin;
private int rightMargin=默认值\u RIGHT\u MARGIN;
/**
*在文本框周围填充
*/
private int-topPadding=默认的\u-TOP\u填充;
private int bottomPadding=默认\u BOTTOM\u PADDING;
private int leftPadding=默认值\u LEFT\u PADDING;
private int rightPadding=默认值\u RIGHT\u PADDING;
/**
*文本框周围水平方向上的空白量
*/
private int totalHorizontalEmptySpace=leftMargin+leftPadding
+右边距+右边距;
/**
*文本框周围垂直方向上的空白量
*/
private int totalVerticalEmptySpace=topMargin+TopAdding
+底部填充+底部边距;
/**
*显示输入文本所需的文本框的最小高度
*/
private int minHeight=getFont().getHeight()+totalVerticalEmptySpace;
/**
*文本框的宽度
*/
private int width=Display.getWidth();
/**
*文本框的高度
*/
私有整数高度=最小高度;
/**
*文本框的背景图像
*/
私人背景图像;
/**
*背景图像的位图版本。
*需要减少由于以下原因而产生的计算开销:
*给定图像的缩放
*并从中导出位图
*每次需要时,都要进行编码。
*/
私有位图位图背景图像;
/**
*此文本框的核心元素
*/
私有EditField;
//私有基本编辑字段editField;
//私有字符串entireText;
公共自定义文本框()
{
//让超类初始化核心
超级(0);
//编辑字段是此管理器的唯一字段。
editField=新的editField();
//editField=新的CustomEditField();
添加(编辑字段);
}
公共自定义文本框(EncodedImage backgroundImage)
{
这个();
背景图像(backgroundImage);
}
公共空白设置大小(整数宽度、整数高度)
{
布尔值isChanged=false;
如果(宽度>0//忽略无效宽度
&&此参数为1.width!=宽度)
{
这个。宽度=宽度;
isChanged=true;
}
//如果指定的高度小于,则忽略该高度
//大于显示文本所需的最小高度。
if(height>minHeight&&height!=此.height)
{
高度=高度;
isChanged=true;
}
//如果宽度/高度已更改,背景图像
//如果可用,请将其调整到新尺寸
if(isChanged==true&&backgroundImage!=null)
{
bitmapBackgroundImage=getScaledBitmapImage(backgroundImage,
此宽度-(左边距+右边距),
该高度-(上边距+下边距));
}
}
公共void setWidth(int-width)
{
如果(宽度>0&&width!=此.width)
{
这个。宽度=宽度;
//如果背景图像可用,请将其调整为新宽度
if(backgroundImage!=null)
{
bitmapBackgroundImage=getScaledBitmapImage(backgroundImage,
此宽度-(左边距+右边距),
该高度-(上边距+下边距));
}
}
}
公共空间设置高度(内部高度)
{
//忽略指定的高度(如果是)
//小于显示文本所需的最小高度。
如果(高度>最小高度)
{
高度=高度;
//如果背景图像可用,请将其调整为新宽度
if(backgroundImage!=null)
{
bitmapBackgroundImage=getScaledBitmapImage(backgroundImage,
此宽度-(左边距+右边距),
该高度-(上边距+下边距));
}
}
}
公共空间背景图像(EncodedImage背景图像)
{
this.backgroundImage=backgroundImage;
//考虑背景图像的高度
//计算文本框的高度。
//setHeight()不确保指定的
//当然,出于正当理由,高度将生效。
//因此,setHeight()方法中位图图像的推导是不确定的。
setHeight(backgroundImage.getHeight()+上边距+下边距);
if(bitmapBackgroundImage==null)
{
bitmapBackgroundImage=getScaledBitmapImage(backgroundImage,
此宽度-(左边距+右边距),
该高度-(上边距+下边距));
}
}
/**
*生成并返回一个
public class CustomTextBox extends Manager
{
    /**
     * Default margins
     */
    private final static int DEFAULT_LEFT_MARGIN = 10;
    private final static int DEFAULT_RIGHT_MARGIN = 10;
    private final static int DEFAULT_TOP_MARGIN = 5;
    private final static int DEFAULT_BOTTOM_MARGIN = 5;

    /**
     * Default paddings
     */
    private final static int DEFAULT_LEFT_PADDING = 10;
    private final static int DEFAULT_RIGHT_PADDING = 10;
    private final static int DEFAULT_TOP_PADDING = 5;
    private final static int DEFAULT_BOTTOM_PADDING = 5;

    /**
     * Margins around the text box
     */
    private int topMargin = DEFAULT_TOP_MARGIN;
    private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
    private int leftMargin = DEFAULT_LEFT_MARGIN;
    private int rightMargin = DEFAULT_RIGHT_MARGIN;

    /**
     * Padding around the text box
     */
    private int topPadding = DEFAULT_TOP_PADDING;
    private int bottomPadding = DEFAULT_BOTTOM_PADDING;
    private int leftPadding = DEFAULT_LEFT_PADDING;
    private int rightPadding = DEFAULT_RIGHT_PADDING;

    /**
     * Amount of empty space horizontally around the text box
     */
    private int totalHorizontalEmptySpace = leftMargin + leftPadding 
                                    + rightPadding + rightMargin;

    /**
     * Amount of empty space vertically around the text box
     */
    private int totalVerticalEmptySpace = topMargin + topPadding 
                                  + bottomPadding + bottomMargin;

    /**
     * Minimum height of the text box required to display the text entered
     */
    private int minHeight = getFont().getHeight() + totalVerticalEmptySpace;

    /**
     * Width of the text box
     */
    private int width = Display.getWidth();

    /**
     * Height of the text box
     */
    private int height = minHeight;

    /**
     * Background image for the text box
     */
    private EncodedImage backgroundImage;

    /**
     * Bitmap version of the backgroundImage.
     * Needed to reduce the calculation overhead incurred by 
     * scaling of the given image
     * and derivation of Bitmap from the 
     * EncodedImage every time it is needed.
     */
    private Bitmap bitmapBackgroundImage;

    /**
     * The core element of this text box
     */
    private EditField editField;
    //private BasicEditField editField;

    //private String entireText;

    public CustomTextBox()
    {
        // Let the super class initialize the core
        super(0);

        // An edit field is the sole field of this manager.
        editField = new EditField();
        //editField = new CustomEditField();
        add(editField);
    }

    public CustomTextBox(EncodedImage backgroundImage)
    {
        this();        
        setBackgroundImage(backgroundImage);
    }

    public void setSize(int width, int height)
    {
        boolean isChanged = false;

        if (width > 0 // Ignore invalid width
                && this.width != width)  
        {
            this.width = width;
            isChanged = true;
        }

        // Ignore the specified height if it is less 
        // than the minimum height required to display the text.
        if (height > minHeight && height != this.height)
        {
            this.height = height;
            isChanged = true;
        }

        // If width/height has been changed and background image 
        // is available, adapt it to the new dimension
        if (isChanged == true && backgroundImage != null)
        {
            bitmapBackgroundImage = getScaledBitmapImage(backgroundImage, 
                                    this.width - (leftMargin+rightMargin),  
                                    this.height - (topMargin+bottomMargin));
        }
    }

    public void setWidth(int width)
    {

        if (width > 0 && width != this.width)
        {
            this.width = width;

            // If background image is available, adapt it to the new width
            if (backgroundImage != null)
            {
                bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
                                        this.width - (leftMargin+rightMargin),
                                        this.height - (topMargin+bottomMargin));
            }
        }
    }

    public void setHeight(int height)
    {
        // Ignore the specified height if it is 
        // less than the minimum height required to display the text.
        if (height > minHeight)
        {
            this.height = height;

            // If background image is available, adapt it to the new width
            if (backgroundImage != null)
            {
                bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
                                        this.width - (leftMargin+rightMargin),  
                                        this.height - (topMargin+bottomMargin));
            }
        }
    }

    public void setBackgroundImage(EncodedImage backgroundImage)
    {
        this.backgroundImage = backgroundImage;

        // Consider the height of background image in 
        // calculating the height of the text box.
        // setHeight() does not ensure that specified 
        // height will be in effect, of course, for valid reasons.
        // So derivation of Bitmap image in the setHeight() method is not sure.
        setHeight(backgroundImage.getHeight() + topMargin + bottomMargin);
        if (bitmapBackgroundImage == null)
        {
            bitmapBackgroundImage = getScaledBitmapImage(backgroundImage, 
                                    this.width - (leftMargin+rightMargin), 
                                    this.height - (topMargin+bottomMargin));
        }
    }

    /**
     * Generate and return a Bitmap Image scaled according 
     * to the specified width and height.
     * 
     * @param image     EncodedImage object
     * @param width     Intended width of the returned Bitmap object
     * @param height    Intended height of the returned Bitmap object
     * @return Bitmap object
     */
    private Bitmap getScaledBitmapImage(EncodedImage image, int width, int height)
    {
        // Handle null image
        if (image == null)
        {
            return null;
        }

        int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
        int currentHeightFixed32 = Fixed32.toFP(image.getHeight());

        int requiredWidthFixed32 = Fixed32.toFP(width);
        int requiredHeightFixed32 = Fixed32.toFP(height);

        int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
        int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);

        image = image.scaleImage32(scaleXFixed32, scaleYFixed32);

        return image.getBitmap();
    }


    protected void sublayout(int width, int height)
    {
        // We have one and only child - the edit field. 
        // Place it at the appropriate place.
        Field field = getField(0);
        layoutChild(field, this.width - totalHorizontalEmptySpace, 
                    this.height - totalVerticalEmptySpace);
        setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);

        setExtent(this.width, this.height);
    }

    public void setTopMargin(int topMargin)
    {
        this.topMargin = topMargin;
    }

    public void setBottomMargin(int bottomMargin)
    {
        this.bottomMargin = bottomMargin;
    }


    protected void paint(Graphics graphics)
    {
        // Draw background image if available, otherwise draw a rectangle
        if (bitmapBackgroundImage == null)
        {
            //graphics.drawRect(leftMargin, topMargin, 
                               // width - (leftMargin+rightMargin), 
                                //height - (topMargin+bottomMargin));
            graphics.drawRoundRect(leftMargin, topMargin, 
                                   width - (leftMargin+rightMargin), 
                                   height - (topMargin+bottomMargin), 5, 5);
        }
        else
        {
            graphics.drawBitmap(leftMargin, topMargin, 
                                width - (leftMargin+rightMargin), 
                                height - (topMargin+bottomMargin),  
                                bitmapBackgroundImage, 0, 0);
        }

        // Determine the rightward text that can fit into the visible edit field

        EditField ef = (EditField)getField(0);
        String entireText = ef.getText();

        boolean longText = false;
        String textToDraw = "";
        Font font = getFont();
        int availableWidth = width - totalHorizontalEmptySpace;
        if (font.getAdvance(entireText) <= availableWidth)
        {
            textToDraw = entireText;
        }
        else
        {
            int endIndex = entireText.length();
            for (int beginIndex = 1; beginIndex < endIndex; beginIndex++)
            {
                textToDraw = entireText.substring(beginIndex);
                if (font.getAdvance(textToDraw) <= availableWidth)
                {
                    longText = true;
                    break;
                }
            }
        }

        if (longText == true)
        {        
            // Force the edit field display only the truncated text
            ef.setText(textToDraw);

            // Now let the components draw themselves
            super.paint(graphics);

            // Return the text field its original text
            ef.setText(entireText);
        }
        else
        {
            super.paint(graphics);
        }
    }

    public int getPreferredWidth()
    {
        return width;
    }

    public int getPreferredHeight()
    {
        return height;
    }

    protected boolean keyChar(char ch, int status, int time)
    {
        if (ch == Characters.ENTER)
        {
            return true;
        }
        else
        {
            return super.keyChar(ch, status, time);
        }
    }

    public String getText()
    {
        return ((EditField)getField(0)).getText();
    }

    public void setText(final String text)
    {
        ((EditField)getField(0)).setText(text);
    }    
}