刷新字段的状态

时间:2011-12-09 11:48:59

标签: blackberry

在我的应用程序中,我使用自定义字段,使用“set ***”方法更改此字段的某些参数(例如,背景图像)。最好的工作,只有一个问题:我正在设置和更改此字段的参数,如下所示:

    record = new UIButton("RECORD", Field.FOCUSABLE, kButtonWidth/3-5, kButtonHeight);
    vfm2.add(Record); //I tryed this befor setters and after: no different
    record.setBackgroundImage("buttonDark.png", "buttonDark.png", "buttonDark.png");
    record.setTitleFontSize(Display.getHeight()/40);
    record.setTitle("RECORD");

当按下具有此字段的屏幕时,我的字段看起来没有调用setter(但它是:我通过日志消息检查了这一点)。 Field的状态只有在被聚焦后才会刷新(我在onFocusonUnFocus上调用相同的setter,我有invalidate())。有没有办法在屏幕上重新显示它?例如,在iPhone SDK中,有viewDidAppear方法,在视图(屏幕)出现时进行校对。黑莓有什么相同的吗?还是其他任何解决方案?


这是我的UIButton类代码:

public class UIButton extends Field 
{       
    private String title = null;
    private Font font;
    private int fontSize;
    private int color;
    private int horizontalAligment;
    private int state;  //0 - normal;   1 - focused;    2 - HightLighted;

    private int height;
    private int width;

    private EncodedImage currentPicture;
    private EncodedImage onPicture;
    private EncodedImage offPicture;
    private EncodedImage lightPicture;

    public UIButton(long style, int Widgh, int Height)
    {
        super(style);

        height = Height;
        width = Widgh;
        fontSize = Display.getHeight()/20;
        FontFamily ff = getFont().getFontFamily();
        font = ff.getFont(0, fontSize);
        title = "";
        color = Color.WHITE;
        state = 0;
        horizontalAligment = DrawStyle.HCENTER;

        onPicture = offPicture = lightPicture = EncodedImage.getEncodedImageResource("buttonDark.png");
        currentPicture = offPicture;

    }

    public String getTitle()
    {
        return title;
    }

    public void setTitleColor (int Color) {
        color = Color;
        invalidate();
    }

    public void setFrame (int Height, int Width) {
        height = Height;
        width = Width;
        invalidate();
    }

    public void setTitle (String Title) {
        title = Title;
        invalidate();
    }

    public void setTitleHorizontalAligment (int hAligment) {
        horizontalAligment = hAligment;
        invalidate();
    }

    public void setBackgroundImage (String forStateNurmal, String forStateFocused, String forStateHightlighted) {
        onPicture = EncodedImage.getEncodedImageResource(forStateFocused);
        offPicture = EncodedImage.getEncodedImageResource(forStateNurmal);
        lightPicture = EncodedImage.getEncodedImageResource(forStateHightlighted);
        invalidate();
    }

    public void setState (int State) {
        state = State;
        switch (state) {
        case 0: {
            currentPicture = offPicture;
            invalidate();
            break;
        }
        case 1: {
            currentPicture = onPicture;
            invalidate();
            break;
        }
        case 2: {
            currentPicture = lightPicture;
            invalidate();
            break;
        }
        }
    }

    public void setTitleFont (Font Font) {
        font = Font;
        invalidate();
    }

    public void setTitleFontSize (int FontSize) {
        fontSize = FontSize;
        FontFamily ff = font.getFontFamily();
        font = ff.getFont(0, fontSize);
        invalidate();
    }

    public int getPreferredHeight() 
    {
        return height;
    }

    public int getPreferredWidth() 
    {
        return  width;
    }


    protected void onFocus(int direction) 
    {
        super.onFocus(direction);
        this.setState(0);
    }


    protected void onUnfocus() 
    {
        if (state!=2) this.setState(1);
    }


    protected void drawFocus(Graphics graphics, boolean on) 
    {
        super.drawFocus(graphics, on);
    }


    protected void layout(int width, int height) 
    {
        setExtent(getPreferredWidth(),getPreferredHeight());
    }


    protected void paint(Graphics graphics) 
    {
        ResizeImage r = new ResizeImage();
        currentPicture = r.sizeImage(currentPicture, width-2, height-2);
        graphics.drawBitmap(1, 1, width-2, height-2, currentPicture.getBitmap(), 0, 0);
        if (title.getBytes().length>0) {

            graphics.setColor(color);
            graphics.setFont(font);

            int x = 0;
            if (horizontalAligment == DrawStyle.LEFT) x = 2;
            graphics.drawText(title, x, (height-font.getHeight())/2, 
                    (int)( getStyle() & DrawStyle.VCENTER & horizontalAligment | DrawStyle.HALIGN_MASK ), width );
        }
    }

    protected boolean navigationClick(int status, int time) 
    {
        fieldChangeNotify(1);
        return true;
    }

}

3 个答案:

答案 0 :(得分:2)

使用小写字母命名本地和字段标识符是Java中非常强大的约定。因此,将“Record”视为局部变量名称非常令人困惑。


如果没有自定义字段的代码UIButton,则无法在此处回答您的问题。根据添加和设置的顺序,BlackBerry OS的内置组件将正常运行,因此您的自定义字段可能不遵循布局和绘画的BlackBerry惯例。

答案 1 :(得分:1)

您忘记在setBackgroundImage()中更改currentPicture。试试currentPicture = offPicture 或致电this.setState(0)中的setBackgroundImage()

答案 2 :(得分:0)

如果您在之前调用set ** methods ,则将该字段添加到管理器中,您首先不应该遇到此问题。你之后有没有理由给他们打电话?