Blackberry 黑莓用户界面提示框

Blackberry 黑莓用户界面提示框,blackberry,blackberry-jde,Blackberry,Blackberry Jde,我对blackberry UI开发非常陌生,我想知道是否有任何方法可以在位图上添加提示框?就像HTML元素中的title属性一样,您可以使用以下工具提示屏幕,并以以下方式向屏幕添加字段 add(new ButtonField(“myButton”), “My Tooltip text”); 工具提示屏幕 import java.util.Timer; import java.util.TimerTask; import java.util.Vector; import net.rim.devi

我对blackberry UI开发非常陌生,我想知道是否有任何方法可以在位图上添加提示框?就像HTML元素中的title属性一样,您可以使用以下工具提示屏幕,并以以下方式向屏幕添加字段

add(new ButtonField(“myButton”), “My Tooltip text”);
工具提示屏幕

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;
    }
}
}
import java.util.Timer;
导入java.util.TimerTask;
导入java.util.Vector;
导入net.rim.device.api.ui.Field;
导入net.rim.device.api.ui.Graphics;
导入net.rim.device.api.ui.XYRect;
导入net.rim.device.api.ui.container.main屏幕;
公共类工具提示屏幕扩展了主屏幕{
工具提示屏幕=此;
布尔值doRedraw=false;//防止infinte重绘
Vector tooltips=new Vector();//用于保存工具提示字符串的向量
专用计时器ToolTimer=新计时器();
专用计时器任务工具脚本任务;
boolean alive=false;//工具提示是否处于活动状态?用于在超时后弹出它
int count=0;//用于计算工具提示显示的时间
//工具提示弹出窗口颜色:
int backgroundcolor=0xeeee;
int bordercolor=0xaaaaaa;
int fontColour=0x666666;
//工具提示:
字符串工具提示;
int工具提示宽度;
国际协调;
int xCoord;
//区域参数:
XYRect内容区;
int contentBottom;
内部内容权;
公共工具提示屏幕(){
超级();
//当超时时间达到100ms*20时,即2秒设置为“假”并重新绘制屏幕:
ToolTiptTask=新时间任务(){
公开募捐{
如果(活着){
计数++;
如果(计数=20){
活着=假;
使无效();
}
}
}
};
ToolTimper.scheduleAtFixedRate(tooltipTask,100100);
}
//override add方法将空字符串添加到工具提示向量:
公共作废添加(字段){
工具提示。添加元素(“”);
super.add(字段);
}
//具有工具提示的字段的自定义添加方法:添加(myField,“myTooltip”);
公共无效添加(字段、字符串工具提示){
super.add(字段);
工具提示。添加元素(工具提示);
}
公共空白设置颜色(int背景色、int边框色、int FontColor){
this.backgroundcolor=背景色;
this.bordercolor=bordercolor;
this.fontColour=fontColour;
}
//当用户更改焦点时重置所有内容,
//可能需要逻辑来检查字段是否已实际更改(对于listfields、objectchoicefields等)
受保护的布尔导航移动(int-dx、int-dy、int-status、int-time){
计数=0;
活着=真;
doRedraw=真;
返回super.navigationMovement(dx、dy、状态、时间);
}
受保护的空心漆(图形){
超级油漆(图形);
如果(活着){
Field focusField=getFieldWithFocus();
tooltip=(字符串)tooltips.elementAt(screen.getFieldWithFocusIndex());
//除非此字段有工具提示,否则不要执行任何超出规范的操作:
如果(!tooltip.equals(“”){
//获取字段内容区域,这可能位于字段实际区域/坐标内:
contentArea=focusField.getContentRect();
contentBottom=contentArea.y+contentArea.height;
contentRight=contentArea.x+contentArea.width;
//+4要在两侧容纳2个像素的填充,请执行以下操作:
tooltipWidth=graphics.getFont().getAdvance(工具提示)+4;
yCoord=contentBottom-focusField.getManager().getVerticalScroll();
//检查工具提示是否完全在屏幕高度内绘制:
如果(yCoord>(getHeight()-30)){
yCoord=getHeight()-30;
}
//检查工具提示是否未从屏幕右侧绘制:
如果(contentRight+tooltipWidth
您可以使用
位图字段
LabelField
,并将它们添加到
垂直字段管理器
水平字段管理器
。您也可以为此编写自定义的
字段
。感谢Rupak的评论,事实上我的屏幕上有一个位图字段。当此位图字段聚焦或悬停时,我只想显示一个标题,就像在twitter、facebook或google+图标上移动鼠标一样,我使用的是JDE6