Custom PopUp to work like tooltip without time out

2019-06-11 06:18发布

问题:

I need to create a Customised PopUp to work like a ToolTip How can i achieve it i have worked on a code please check out and tell me where i need improvement

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


        }

    }

i get an error of NullPointer Exception at super(vfm) because vfm is null when i use it as follows . How can i optimise my code .

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

回答1:

Pass an initialized instance of VerticalFieldManager to super(..) of PopupScreen. Check following code.

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


Updated code for custom background support

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

You need to use getDelegate().setBackground(..) to set the background. Check the class BackgroundFactory for creating custom background.