黑莓 UI 提示框

发布于 2025-01-01 17:37:35 字数 61 浏览 1 评论 0原文

我对黑莓 UI 开发非常陌生,想知道有什么方法可以在位图上添加提示框吗?就像 HTML 元素中的标题属性一样

I am very much new to the blackberry UI development and want to know that is there any way you can add a hint box on bitmap? just like a title attribute in an element of HTML

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

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

发布评论

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

评论(1

月寒剑心 2025-01-08 17:37:35

您可以使用以下 TooltipScreen 并通过以下方式将字段添加到屏幕

add(new ButtonField(“myButton”), “My Tooltip text”);

TooltipScreen

import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.container.MainScreen;

public class TooltipScreen extends MainScreen {

TooltipScreen screen = this;
boolean doRedraw = false;//prevent infinte redrawing
Vector tooltips = new Vector();//vector to hold tooltip strings
private Timer tooltipTimer = new Timer();
private TimerTask tooltipTask;
boolean alive = false;//is the tooltip alive? used to pop it after our timeout
int count = 0;//used to calculate time tooltip is displayed
//tooltip popup colours:
int backgroundColour = 0xeeeeee;
int borderColour = 0xaaaaaa;
int fontColour = 0x666666;
//the tooltip:
String tooltip;
int tooltipWidth;
int yCoord;
int xCoord;
//region parameters:
XYRect contentArea;
int contentBottom;
int contentRight;

public TooltipScreen() {
    super();

    //when timeout reaches 100ms*20 ie. 2seconds set alive to false and redraw screen:
    tooltipTask = new TimerTask() {

        public void run() {
            if (alive) {
                count++;
                if (count == 20) {
                    alive = false;
                    invalidate();
                }
            }
        }
    };

    tooltipTimer.scheduleAtFixedRate(tooltipTask, 100, 100);

}

//override add method adds an empty string to tooltip vector:
public void add(Field field) {
    tooltips.addElement("");
    super.add(field);
}

//custom add method for fields with tooltip: add(myField, "myTooltip");
public void add(Field field, String tooltip) {
    super.add(field);
    tooltips.addElement(tooltip);
}

public void setColours(int backgroundColour, int borderColour, int fontColour) {
    this.backgroundColour = backgroundColour;
    this.borderColour = borderColour;
    this.fontColour = fontColour;
}

//reset everything when user changes focus,
//possibly needs logic to check field has actually changed (for listfields, objectchoicefields etc etc)
protected boolean navigationMovement(int dx, int dy, int status, int time) {
    count = 0;
    alive = true;
    doRedraw = true;
    return super.navigationMovement(dx, dy, status, time);
}

protected void paint(Graphics graphics) {
    super.paint(graphics);
    if (alive) {
        Field focusField = getFieldWithFocus();
        tooltip = (String) tooltips.elementAt(screen.getFieldWithFocusIndex());

        //don't do anything outside the norm unless this field has a tooltip:
        if (!tooltip.equals("")) {
            //get the field content region, this may fall inside the field actual region/coordinates:
            contentArea = focusField.getContentRect();
            contentBottom = contentArea.y + contentArea.height;
            contentRight = contentArea.x + contentArea.width;

            //+4 to accomodate 2 pixel padding on either side:
            tooltipWidth = graphics.getFont().getAdvance(tooltip) + 4;

            yCoord = contentBottom - focusField.getManager().getVerticalScroll();
            //check the tooltip is being drawn fully inside the screen height:
            if (yCoord > (getHeight() - 30)) {
                yCoord = getHeight() - 30;
            }

            //check the tooltip doesn't get drawn off the right side of the screen:
            if (contentRight + tooltipWidth < getWidth()) {
                xCoord = contentRight;
            } else {
                xCoord = getWidth() - tooltipWidth;
            }

            //draw the tooltip
            graphics.setColor(backgroundColour);
            graphics.fillRect(xCoord, yCoord, tooltipWidth, 30);
            graphics.setColor(borderColour);
            graphics.drawRect(xCoord, yCoord, tooltipWidth, 30);
            graphics.setColor(fontColour);
            graphics.drawText(tooltip, xCoord + 2, yCoord);
        }
    }
    //doRedraw logic prevents infinite loop
    if (doRedraw) {
        //System.out.println("redrawing screen: " + System.currentTimeMillis());
        screen.invalidate();
        doRedraw = false;
    }
}
}

You can use the following TooltipScreen and add fields to the screen in the following way

add(new ButtonField(“myButton”), “My Tooltip text”);

TooltipScreen

import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.container.MainScreen;

public class TooltipScreen extends MainScreen {

TooltipScreen screen = this;
boolean doRedraw = false;//prevent infinte redrawing
Vector tooltips = new Vector();//vector to hold tooltip strings
private Timer tooltipTimer = new Timer();
private TimerTask tooltipTask;
boolean alive = false;//is the tooltip alive? used to pop it after our timeout
int count = 0;//used to calculate time tooltip is displayed
//tooltip popup colours:
int backgroundColour = 0xeeeeee;
int borderColour = 0xaaaaaa;
int fontColour = 0x666666;
//the tooltip:
String tooltip;
int tooltipWidth;
int yCoord;
int xCoord;
//region parameters:
XYRect contentArea;
int contentBottom;
int contentRight;

public TooltipScreen() {
    super();

    //when timeout reaches 100ms*20 ie. 2seconds set alive to false and redraw screen:
    tooltipTask = new TimerTask() {

        public void run() {
            if (alive) {
                count++;
                if (count == 20) {
                    alive = false;
                    invalidate();
                }
            }
        }
    };

    tooltipTimer.scheduleAtFixedRate(tooltipTask, 100, 100);

}

//override add method adds an empty string to tooltip vector:
public void add(Field field) {
    tooltips.addElement("");
    super.add(field);
}

//custom add method for fields with tooltip: add(myField, "myTooltip");
public void add(Field field, String tooltip) {
    super.add(field);
    tooltips.addElement(tooltip);
}

public void setColours(int backgroundColour, int borderColour, int fontColour) {
    this.backgroundColour = backgroundColour;
    this.borderColour = borderColour;
    this.fontColour = fontColour;
}

//reset everything when user changes focus,
//possibly needs logic to check field has actually changed (for listfields, objectchoicefields etc etc)
protected boolean navigationMovement(int dx, int dy, int status, int time) {
    count = 0;
    alive = true;
    doRedraw = true;
    return super.navigationMovement(dx, dy, status, time);
}

protected void paint(Graphics graphics) {
    super.paint(graphics);
    if (alive) {
        Field focusField = getFieldWithFocus();
        tooltip = (String) tooltips.elementAt(screen.getFieldWithFocusIndex());

        //don't do anything outside the norm unless this field has a tooltip:
        if (!tooltip.equals("")) {
            //get the field content region, this may fall inside the field actual region/coordinates:
            contentArea = focusField.getContentRect();
            contentBottom = contentArea.y + contentArea.height;
            contentRight = contentArea.x + contentArea.width;

            //+4 to accomodate 2 pixel padding on either side:
            tooltipWidth = graphics.getFont().getAdvance(tooltip) + 4;

            yCoord = contentBottom - focusField.getManager().getVerticalScroll();
            //check the tooltip is being drawn fully inside the screen height:
            if (yCoord > (getHeight() - 30)) {
                yCoord = getHeight() - 30;
            }

            //check the tooltip doesn't get drawn off the right side of the screen:
            if (contentRight + tooltipWidth < getWidth()) {
                xCoord = contentRight;
            } else {
                xCoord = getWidth() - tooltipWidth;
            }

            //draw the tooltip
            graphics.setColor(backgroundColour);
            graphics.fillRect(xCoord, yCoord, tooltipWidth, 30);
            graphics.setColor(borderColour);
            graphics.drawRect(xCoord, yCoord, tooltipWidth, 30);
            graphics.setColor(fontColour);
            graphics.drawText(tooltip, xCoord + 2, yCoord);
        }
    }
    //doRedraw logic prevents infinite loop
    if (doRedraw) {
        //System.out.println("redrawing screen: " + System.currentTimeMillis());
        screen.invalidate();
        doRedraw = false;
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文