调用onUnfocus()时启动paint()

时间:2011-02-24 09:13:12

标签: blackberry

调用onFocus()方法时应该启动重绘吗?下面是一个按钮,如果选择该按钮,则绘制不同的背景位图,如果选择了另一个按钮,则应绘制新的背景位图。但是这种情况没有发生,一旦选中,按钮仍保留其高亮位图,即使没有焦点也是如此。

以下是我致电onUnFocus()的方式:

        protected void onUnfocus() {
            this.setHighlight(false);
            super.onUnfocus();

        }

这是我的完整课程:

/**
 * Button field with a bitmap as its label.
 */
public class SelectQuizTypeButtonField extends BitmapButtonField {
        private Bitmap backgroundBitmap;
        private Bitmap backgroundBitmapHighlight;
        private Bitmap contestantImageBitmap;
        private Bitmap statusBitmap;
        private Bitmap costBitmap;
        private Bitmap timerBitmap;
        private boolean highlighted = false;
        private String label;
        private Font font;
        private Graphics graphics;
        /**
         * Instantiates a new bitmap button field.
         * 
         * @param backgroundBitmap the bitmap to use as a label
         */
        public SelectQuizTypeButtonField(
                Bitmap backgroundBitmap, 
                Bitmap backgroundBitmapHighlight, 
                Bitmap contestantImageBitmap,
                Bitmap costBitmap,
                Bitmap statusBitmap, 
                Bitmap timerBitmap,
                String label, int width, Font font) {
            this(backgroundBitmap, backgroundBitmapHighlight, ButtonField.CONSUME_CLICK|ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER
                    , label, width, font, contestantImageBitmap, statusBitmap,
                    costBitmap, timerBitmap);

        }



        public SelectQuizTypeButtonField(Bitmap backgroundBitmap, 
                Bitmap backgroundBitmapHighlight, long style, 
                String label, int width, Font font, Bitmap contestantImageBitmap, 
                Bitmap statusBitmap, Bitmap costBitmap, Bitmap timerBitmap) {
            super(backgroundBitmap , backgroundBitmapHighlight);
            this.backgroundBitmap = backgroundBitmap;
            this.backgroundBitmapHighlight = backgroundBitmapHighlight;
            this.label = label;
            this.font = font;
            this.contestantImageBitmap = contestantImageBitmap;
            this.statusBitmap = statusBitmap;
            this.costBitmap = costBitmap;
            this.timerBitmap = timerBitmap;
        }

        protected void onFocus(int direction) {

            this.setHighlight(true);
            super.onFocus(direction);

            }

            protected void onUnfocus() {
                this.setHighlight(false);
                super.onUnfocus();

            }
        /* (non-Javadoc)
         * @see net.rim.device.api.ui.component.ButtonField#layout(int, int)
         */
        protected void layout(int width, int height) {
                setExtent(getPreferredWidth(), getPreferredHeight());
        }

        /* (non-Javadoc)
         * @see net.rim.device.api.ui.component.ButtonField#getPreferredWidth()
         */
        public int getPreferredWidth() {
                return backgroundBitmap.getWidth();
        }

        /* (non-Javadoc)
         * @see net.rim.device.api.ui.component.ButtonField#getPreferredHeight()
         */
        public int getPreferredHeight() {
                return backgroundBitmap.getHeight();
        }

        /* (non-Javadoc)
         * @see net.rim.device.api.ui.component.ButtonField#paint(net.rim.device.api.ui.Graphics)
         */
        protected void paint(Graphics graphics) {

                this.graphics = graphics;
                int width = backgroundBitmap.getWidth();
                int height = backgroundBitmap.getHeight();
                Bitmap backgroundBitM = backgroundBitmap;
                if (highlighted)
                    backgroundBitM = backgroundBitmapHighlight;

                graphics.fillRoundRect(0, 0, width, height, 10, 10);  
                graphics.drawBitmap(0, 0, width, height, backgroundBitM, 0, 0);

                int quarter = backgroundBitM.getHeight() / 4;
                graphics.drawBitmap(10, 10, contestantImageBitmap.getWidth(), contestantImageBitmap.getHeight(), contestantImageBitmap, 0, 0);
                graphics.drawBitmap(80, quarter*2, costBitmap.getWidth(), costBitmap.getHeight(), costBitmap ,0, 0);
              //  graphics.drawBitmap(30, quarter*3, width, height, timerBitmap, 0, 0);
                graphics.drawBitmap(backgroundBitM.getWidth()-statusBitmap.getWidth(), 0, width, height, statusBitmap, 0, 0);
                graphics.setFont(font);
                graphics.drawText(label, 0, backgroundBitmap.getHeight());  

        }


        public void setHighlight(boolean highlight)
        {
            this.highlighted = highlight;      
        }

        protected void drawFocus( Graphics g, boolean on ) {
            // Paint() handles it all
            g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
            paintBackground( g );
            paint( g );
        }
}

2 个答案:

答案 0 :(得分:1)

您是否尝试过调用invalidate()方法?此方法强制屏幕或字段在运行时重绘自身。尝试将其添加到您的unFocus方法中,如此;

    protected void onUnfocus() {
        this.setHighlight(false);
        invalidate();
        super.onUnfocus();
    }

答案 1 :(得分:0)

您也可以在drawFocus方法中执行此操作。这就是为什么它有boolean on参数。

protected void drawFocus( Graphics g, boolean on ) {
    // Paint() handles it all
    if (on) {
        g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
    } else {
        g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, false );
    }
    // Or to keep it short: 
    g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, on );

    paintBackground( g );
    paint( g );
}