修改类,使ButtonField无法选择(永远不会有焦点)

时间:2011-02-17 19:53:42

标签: blackberry

如何修改下面的类以使按钮字段不可选(从不具有焦点),因此从不突出显示?

由于

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.component.ButtonField;

/**
 * Button field with a bitmap as its label.
 */
public class BitmapField extends ButtonField {
        private Bitmap bitmap;
        private Bitmap bitmapHighlight;
        private boolean highlighted = false;
        private int width;
        private String label;
        private Font font;

        /**
         * Instantiates a new bitmap button field.
         * 
         * @param bitmap the bitmap to use as a label
         */
        public BitmapField(Bitmap bitmap, Bitmap bitmapHighlight, String label, int width, Font font) {
            this(bitmap, bitmapHighlight, ButtonField.CONSUME_CLICK|ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER
                    , label, width, font);

        }

        protected void onFocus(int direction) {

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

            }

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

            }


        public BitmapField(Bitmap bitmap, Bitmap bitmapHighlight, long style, 
                String label, int width, Font font) {
            super(style);
            this.bitmap = bitmap;
            this.bitmapHighlight = bitmapHighlight;
            this.width = width;
            this.label = label;
            this.font = font;
        }

        /* (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 bitmap.getWidth()+this.width;
        }

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

        /* (non-Javadoc)
         * @see net.rim.device.api.ui.component.ButtonField#paint(net.rim.device.api.ui.Graphics)
         */
        protected void paint(Graphics graphics) {
                super.paint(graphics);
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                Bitmap b = bitmap;
                if (highlighted)
                    b = bitmapHighlight;

                //graphics.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);  
                graphics.drawBitmap(0, 0, width, height, b, 0, 0);
                graphics.setFont(font);
                graphics.drawText(label, 0, bitmap.getHeight());  
        }

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

1 个答案:

答案 0 :(得分:1)

将样式位Field.NON_FOCUSABLE添加到构造函数中。