在黑莓上自定义编辑字段

发布于 2024-12-14 04:46:23 字数 13807 浏览 0 评论 0原文

我想像这个链接一样自定义编辑字段 http://www.hostingpics.net /viewer.php?id=44669634im.png。 我找到了这段代码

------------------------------------------------CustomTextBox---------------------
        package mypackage;
        import net.rim.device.api.ui.Manager;
        import net.rim.device.api.ui.Field;
        import net.rim.device.api.ui.component.EditField;
        import net.rim.device.api.ui.component.BasicEditField;
        import net.rim.device.api.system.EncodedImage;
        import net.rim.device.api.system.Bitmap;
        import net.rim.device.api.system.Display;
        import net.rim.device.api.ui.Graphics;
        import net.rim.device.api.system.Characters;
        import net.rim.device.api.math.Fixed32;
        import net.rim.device.api.ui.DrawStyle;
        import net.rim.device.api.ui.Font;

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

--------------------------------------------MyScreen------------------------

    package mypackage;

    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.container.MainScreen;

    /**
     * A class extending the MainScreen class, which provides default standard
     * behavior for BlackBerry GUI applications.
     */
    public final class MyScreen extends MainScreen
    {
         public MyScreen()
            {
           new CustomTextBox();
              }
    }
--------------------------------------------MyApp------------------------


    package mypackage;

    import net.rim.device.api.ui.UiApplication;

    /**
     * This class extends the UiApplication class, providing a
     * graphical user interface.
     */
    public class MyApp extends UiApplication
    {

        public static void main(String[] args)
        {
            // Create a new instance of the application and make the currently
            // running thread the application's event dispatch thread.
            MyApp theApp = new MyApp();       
            theApp.enterEventDispatcher();
        }



        public MyApp()
        {        
            // Push a screen onto the UI stack for rendering.
            pushScreen(new MyScreen());
        }    
    }

,但是我获得了白屏,我应该在这段代码中更改什么才能保留自定义编辑字段

I want to customize Edit Field like in this link http://www.hostingpics.net/viewer.php?id=44669634im.png.
I find this code

------------------------------------------------CustomTextBox---------------------
        package mypackage;
        import net.rim.device.api.ui.Manager;
        import net.rim.device.api.ui.Field;
        import net.rim.device.api.ui.component.EditField;
        import net.rim.device.api.ui.component.BasicEditField;
        import net.rim.device.api.system.EncodedImage;
        import net.rim.device.api.system.Bitmap;
        import net.rim.device.api.system.Display;
        import net.rim.device.api.ui.Graphics;
        import net.rim.device.api.system.Characters;
        import net.rim.device.api.math.Fixed32;
        import net.rim.device.api.ui.DrawStyle;
        import net.rim.device.api.ui.Font;

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

--------------------------------------------MyScreen------------------------

    package mypackage;

    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.container.MainScreen;

    /**
     * A class extending the MainScreen class, which provides default standard
     * behavior for BlackBerry GUI applications.
     */
    public final class MyScreen extends MainScreen
    {
         public MyScreen()
            {
           new CustomTextBox();
              }
    }
--------------------------------------------MyApp------------------------


    package mypackage;

    import net.rim.device.api.ui.UiApplication;

    /**
     * This class extends the UiApplication class, providing a
     * graphical user interface.
     */
    public class MyApp extends UiApplication
    {

        public static void main(String[] args)
        {
            // Create a new instance of the application and make the currently
            // running thread the application's event dispatch thread.
            MyApp theApp = new MyApp();       
            theApp.enterEventDispatcher();
        }



        public MyApp()
        {        
            // Push a screen onto the UI stack for rendering.
            pushScreen(new MyScreen());
        }    
    }

But I obtain white Screen what should I change in this code to ibtain the custon edit field

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ペ泪落弦音 2024-12-21 04:46:23

您好,您刚刚创建了对象,但您忘记将该对象添加到主屏幕,

package mypackage;

import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
     public MyScreen()
        {
       add(new CustomTextBox());//in your code it is like new CustomTextBox();
          }
}

如果您想将任何图像作为背景添加到编辑框,那么您可以按照以下方式

打包 mypackage;

import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
     public MyScreen()
        {
             EncodedImage enc_img=EncodedImage.getEncodedImageResource("input-box.png");//image name is 'input-box.png'
   CustomTextBox edi_box=new CustomTextBox(enc_img);
    add(edi_box);
       }
}

hi you just create object but you forget to add that object to mainscreen

package mypackage;

import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
     public MyScreen()
        {
       add(new CustomTextBox());//in your code it is like new CustomTextBox();
          }
}

if you want to add any image as background to your editbox then you can yous following way

package mypackage;

import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
     public MyScreen()
        {
             EncodedImage enc_img=EncodedImage.getEncodedImageResource("input-box.png");//image name is 'input-box.png'
   CustomTextBox edi_box=new CustomTextBox(enc_img);
    add(edi_box);
       }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文