0

我需要创建一个自定义弹出窗口来像工具提示一样工作我如何实现它我已经处理过代码请检查并告诉我我需要改进的地方

import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;


    public class ToolTip extends PopupScreen{

        private static VerticalFieldManager vfm;
        private LabelField lbl;
        private int xPosition;
        private int yPosition;
        private String message;
        public ToolTip(String message,int xPos,int yPos){
            super(vfm);
            this.xPosition=xPos;
            this.yPosition=yPos;
            this.message=message;
            vfm=new VerticalFieldManager(){
                protected void paint(Graphics graphics) {
                    graphics.setColor(0x00FFFFFF); 
                    graphics.fillRect(0,0,getWidth(),getHeight());
                    graphics.setColor(0x00000000);
                    graphics.drawRect(0,0,getWidth(),getHeight());
                    super.paint(graphics);
                }       

            };

            lbl=new LabelField(message);
            vfm.add(lbl);
        }

        protected void sublayout(int width, int height) {
            super.sublayout(width, height);
            setExtent(lbl.getPreferredWidth(), lbl.getPreferredHeight());
            setPosition(xPosition, yPosition);


        }

    }

我收到 NullPointer Exception 的错误,super(vfm)因为当我按如下方式使用 vfm 时它为空。我怎样才能优化我的代码。

ButtonField bf1=new ButtonField("Login"){
            protected boolean navigationClick(int status, int time) {

            UiApplication.getUiApplication().pushScreen(new ToolTip("Hello ", 50, 50));
                return super.navigationClick(status, time);
            }

        };
        add(bf1);
4

1 回答 1

1

VerticalFieldManager传递to super(..)of的初始化实例PopupScreen。检查以下代码。

class ToolTip extends PopupScreen {
    private static VerticalFieldManager vfm = new VerticalFieldManager() {
        protected void paint(Graphics graphics) {
            graphics.setColor(0x00FFFFFF);
            graphics.fillRect(0, 0, getWidth(), getHeight());
            graphics.setColor(0x00000000);
            graphics.drawRect(0, 0, getWidth(), getHeight());
            super.paint(graphics);
        }
    };

    // other codes

    public ToolTip(String message, int xPos, int yPos) {
        super(vfm);
        // other codes
    }

    protected void sublayout(int width, int height) {
        // other codes
    }
}


更新了自定义背景支持的代码

class ToolTip extends PopupScreen {
    private int xPosition;
    private int yPosition;

    public ToolTip(String message, int xPos, int yPos) {
        super(new VerticalFieldManager());

        // Define and set background here
        // Use - BackgroundFactory.createBitmapBackground(bitmap) for bitmap background
        getDelegate().setBackground(BackgroundFactory.createSolidBackground(Color.RED));

        // Add other UI Field here
        this.xPosition=xPos;
        this.yPosition=yPos;

        add(new LabelField(message));
        // other codes
    }

    // Empty implementation of this will disable default rounded border.
    protected void applyTheme() {
    }

    protected void sublayout(int width, int height) {
        super.sublayout(width, height);
        setPosition(xPosition, yPosition);
    }
}

您需要使用getDelegate().setBackground(..)来设置背景。检查BackgroundFactory用于创建自定义背景的类。

于 2012-06-12T10:05:44.073 回答