BlackBerry中的图像按钮

Rah*_*rma 8 blackberry java-me

如何在BlackBerry中实施图像按钮?

ref*_*log 20

在这里,完整的代码:

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

/**
 * Button field with a bitmap as its label.
 */
public class BitmapButtonField extends ButtonField {
        private Bitmap bitmap;
        private Bitmap bitmapHighlight;
        private boolean highlighted = false;

        /**
         * Instantiates a new bitmap button field.
         * 
         * @param bitmap the bitmap to use as a label
         */
        public BitmapButtonField(Bitmap bitmap, Bitmap bitmapHighlight) {
            this(bitmap, bitmapHighlight, ButtonField.CONSUME_CLICK|ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER);
        }

        public BitmapButtonField(Bitmap bitmap, Bitmap bitmapHighlight, long style) {
            super(style);
            this.bitmap = bitmap;
            this.bitmapHighlight = bitmapHighlight;
        }

        /* (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();
        }

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

        /* (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.drawBitmap(0, 0, width, height, b, 0, 0);
        }

        public void setHighlight(boolean highlight)
        {
            this.highlighted = highlight;           
        }
}
Run Code Online (Sandbox Code Playgroud)

  • 我知道这会回来一段时间,但我需要添加这些方法来实现这个工作 - 受保护的void onFocus(int direction){this.setHighlight(true); super.onFocus(方向); } protected void onUnfocus(){this.setHighlight(false); super.onUnfocus(); } (3认同)

Ray*_*hey 7

使用RIM的高级UI包.

http://supportforums.blackberry.com/t5/Java-Development/Implement-advanced-buttons-fields-and-managers/ta-p/488276

它包含一个BitmapButton字段和大量有用的UI工具.

(毫无疑问,Reflogs示例很好,但我认为对于登陆此页面的新BB开发人员而言,高级UI包更有利)