我如何在Blackberry中创建新的TextArea

时间:2012-03-30 11:59:27

标签: blackberry

我是黑莓新手,我想创建一个自定义textArea。输入40个字母后,它应自动更改该行。在更多行之后,显示垂直滚动选项。 请任何给我发送代码或告诉我程序

1 个答案:

答案 0 :(得分:0)

package max;




import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.*;

public class TextArea extends VerticalFieldManager implements FocusChangeListener, FieldChangeListener {


    /**
     * Default margins
     */
    private final static int DEFAULT_LEFT_MARGIN = 1;
    private final static int DEFAULT_RIGHT_MARGIN =5;
    private final static int DEFAULT_TOP_MARGIN = 0;
    private final static int DEFAULT_BOTTOM_MARGIN = 0;

    /**
     * Default paddings
     */
    private final static int DEFAULT_LEFT_PADDING = 0;
    private final static int DEFAULT_RIGHT_PADDING =0;
    private final static int DEFAULT_TOP_PADDING =0;
    private final static int DEFAULT_BOTTOM_PADDING=0;
    //private static final int TYPE_PASSWORD = 0;


    /**
     * 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;
    private boolean ignoreChange = false;

    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;
    private EncodedImage focusedBackgroundImage;


    /**
     * 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 int focus_color = AppColors.FOCUS_COLOR;
    private int focus_color = AppColors.FOCUS_COLOR;
    private Bitmap bitmapBackgroundImage;
    private Bitmap focusedBitmapBackgroundImage;

    //private EditField editField;
    /**
     * The core element of this text box
     */
//    public static final int TYPE_PASSWORD=2;
//    public static final int TYPE_PLAIN=1;
   // private BasicEditField editField;

//    public TextArea(int type)
//    {
//        // Let the super class initialize the core
//        super(0);
////        this.type = type;
//        VerticalFieldManager vfm=new VerticalFieldManager(Manager.VERTICAL_SCROLL);
//        if (type!=TYPE_PASSWORD) {
//          // An edit field is the sole field of this manager.
//          editField = new EditField(FIELD_VCENTER);
//      }else{
//          editField = new PasswordEditField();
//          editField.setMaxSize(12);
//      }
//      //editField = new CustomEditField();
//        vfm.add(editField);
//      add(vfm);
//        vfm.setFocusListener(this);
//       
//    }

    public EditField  objEditField;

    public TextArea(int w,int h, EncodedImage backgroundImage, EncodedImage focusedState)
    {
        this.width=w;
        this. height=h;   
        VerticalFieldManager vfm=new VerticalFieldManager(Manager.VERTICAL_SCROLL);
        setFocusedBackgroundImage(backgroundImage);
        if(focusedState==null){
            focusedState = backgroundImage;
        }
        setFocusedBackgroundImage(focusedState);

        vfm.add(objEditField);
        add(vfm);
        vfm.setFocusListener(this);
       //
        objEditField.setFocusListener(this);
    }

//  private void setFocusedBackgroundImage(EncodedImage focusedState) {
//      // TODO Auto-generated method stub
//      
//  }

    public TextArea(int w,int h) {
        // TODO Auto-generated constructor stub
      super(Manager.NO_HORIZONTAL_SCROLL);
     this.width=w;
     this.height=h;

      VerticalFieldManager vfm=new VerticalFieldManager(Manager.VERTICAL_SCROLL);

      objEditField=new EditField()
      {
//        public void paint(Graphics g)
//        {
//            getManager().invalidate();
//            super.paint(g);
//            
//        }


      };

        vfm.add(objEditField);
        add(vfm);
    }

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

                focusedBitmapBackgroundImage = getScaledBitmapImage(focusedBackgroundImage, 
                        this.width - (leftMargin+rightMargin),  
                        this.height - (topMargin+bottomMargin));
            }
//          int actualHeight = getFont().getHeight();
//          if(height>actualHeight){
//              yTranslate = (height - actualHeight)/2;
//          }
        }

     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));
                    focusedBitmapBackgroundImage = getScaledBitmapImage(focusedBackgroundImage, 
                            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));
                    focusedBitmapBackgroundImage = getScaledBitmapImage(focusedBackgroundImage, 
                            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));
            }
        }

     public void setFocusedBackgroundImage(EncodedImage backgroundImage)
        {
            this.focusedBackgroundImage = 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 (focusedBitmapBackgroundImage == null)
            {
                focusedBitmapBackgroundImage = getScaledBitmapImage(backgroundImage, 
                                        this.width - (leftMargin+rightMargin), 
                                        this.height - (topMargin+bottomMargin));
            }
        }

     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 paint(Graphics g)
    {

//      super.paint(g);
//      if(isFocus()){
//          g.setColor(Color.AQUA);
//      }else{
//          g.setColor(Color.BLACK);
//      }
//      //g.drawRect(0, 0, getWidth(), getHeight());
//      g.drawRoundRect(leftMargin, topMargin, 
//                width - (leftMargin+rightMargin), 
//                height - (topMargin+bottomMargin), 5, 5);

        int prevColor = g.getColor();

        if (bitmapBackgroundImage == null)
        {
            //graphics.drawRect(leftMargin, topMargin,  width - (leftMargin+rightMargin), height - (topMargin+bottomMargin));

            if(isFocus()){
                g.setColor(focus_color);

            }
            else{
                g.setColor(Color.BLACK);
            }


            g.drawRoundRect(leftMargin, topMargin, 
                                   this.width - (leftMargin+rightMargin), 
                                   this.height - (topMargin+bottomMargin), 5, 5);
            g.setColor(Color.BLACK);

            invalidate();


        }
        else
        {
            if(isFocus()){
                g.setColor(Color.AQUA);
            //  g.setColor(Color.WHITE);
            g.drawBitmap(leftMargin, topMargin, 
                               this.width - (leftMargin+rightMargin), 
                               this.height - (topMargin+bottomMargin),  
                                focusedBitmapBackgroundImage, 0, 0);


            }else{
                 g.setColor(Color.BLACK);
                g.drawBitmap(leftMargin, topMargin, 
                        this.width- (leftMargin+rightMargin), 
                         this.height - (topMargin+bottomMargin),  
                        bitmapBackgroundImage, 0, 0);

            }
        }
        EditField objEditField=new EditField();
         String entireText = objEditField.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
                objEditField.setText(textToDraw);

                // Now let the components draw themselves
//              graphics.translate(0, yTranslate);
                super.paint(g);
//              graphics.translate(0, -yTranslate);

                // Return the text field its original text
                objEditField.setText(entireText);
            }
            else
            {
//              graphics.translate(0, yTranslate);
                super.paint(g);
//              graphics.translate(0, -yTranslate);
            }
            //g.setColor(Color.WHITE);
            g.setColor(prevColor);
        }


     protected void sublayout(int width1, int height1)
        {
            // We have one and only child - the edit field. 
            // Place it at the appropriate place.
         if(width==0)
         {
             width=width1;

         }
         if(height==0)
         {

             height=height1;

         }
//       super.sublayout(this.width - totalHorizontalEmptySpace,this.height - totalVerticalEmptySpace);
//       setExtent(this.width, this.height);

         Field field = getField(0);
            layoutChild(field, this.width - totalHorizontalEmptySpace, 
                        this.height - totalVerticalEmptySpace);
            setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);

            setExtent(this.width, this.height);

        }

     public int getPreferredWidth()
        {
            return width;
        }

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

     public void setBottomMargin(int bottomMargin)
        {
            this.bottomMargin = bottomMargin;
        }
//   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 objEditField.getText();

    }

    public void setText(String str)
    {
        objEditField.setText(str);

    }
    public void setChangeListner(){
         objEditField.setChangeListener(new FieldChangeListener() {

            public void fieldChanged(Field field, int context) {

            }
        });
    }

    public void fieldChanged(Field field, int context) {

        if (!ignoreChange) {
            EditField textField = (EditField) field;
            textChanged(textField.getText());
        }
    }

public void textChanged(String text){

    }
public void startListning(){
    ignoreChange = false;
}
    public void focusChanged(Field field, int eventType) {
          invalidate();

        }

    public void addTextChangeListner(){
        objEditField.setChangeListener(this);
    }

    public void removeTextChangeListner() {
        objEditField.setChangeListener(null);
    }

}


I thinks this code will help you.