ITeye博客

黑莓开发学习入门系列,自己动手实现一个日历软件(五)

bulktree · 2011-01-20 15:45 · [网站分类]1.网站首页原创Java技术区(对首页文章的要求: 原创、高质量、经过认真思考并精心写作。BlogJava管理团队会对首页的文章进行管理。) · Markdown

如何自定义一个自己的UI组件?

创建一个自定义UI,需要继承自现有的组件或者直接继承Field类,通常我们必须实现layout()和paint()方法用于显示想要的UI界面。

1. layout()方法可在手机屏幕上实现一个具有宽度和高度的区域,调用setExtent(width, height);实现。getPreferredWidth()、getPreferredHeight()告诉容器出现在屏幕上合适的高度和宽度

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->public int getPreferredWidth() {
        return this.getScreen().getWidth() / 7;
    }

    /**
     * Gets the preferred height of the button.
     */
    public int getPreferredHeight() {
        return _labelHeight;
    }

    protected void layout(int width, int height) {
        // Calc width.
        width = getPreferredWidth();

        // Calc height.
        height =getPreferredHeight();

        // Set dimensions.
        setExtent(width, height);
    }

2. paint()方法使用Graphics对象绘制UI,drawLine, drawRect,drawText

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->protected void paint(Graphics graphics) {
//        graphics.setColor(0xDDDDDD);
//        graphics.fillRect( 0, 0, getWidth(), getHeight() );
        int textX, textY, textWidth;
        int w = getWidth();
        if (_isBorder == 0) {
            graphics.drawRect(0, 0, w, getHeight());
        }
        textX = 4;
        textY = 2;
        textWidth = w - 6;
        graphics.drawText(_label, textX, textY, (int) (getStyle() 
                & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK), textWidth);
    }

3. 如果要处理键盘和滚轮事件可以实现keyChar()/trackwheelClick()方法

4. 如果在控件获取到焦点,实现onFocus()方法,可查看黑莓自带的例子

贴上完整的代码:CalenderField.java

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->package org.bulktree.calender;

import net.rim.device.api.ui.*;

class CalenderField extends Field implements DrawStyle {
    public static final int RECTANGLE = 1;
    public static final int TRIANGLE = 2;
    public static final int OCTAGON = 3;

    private String _label;
    private Font _font;
    private int _labelHeight;
    private int _isBorder = 0;//是否有边框0有1无
    
    public CalenderField(String label, int shape, long style) {
        super(style);
        _label = label;
        _font = getFont();
        _labelHeight = _font.getHeight();
    }
    
    public CalenderField(String lable, int shape, long style, int isBorder) {
        super(style);
        _label = lable;
        _font = getFont();
        _labelHeight = _font.getHeight();
        _isBorder = isBorder;
    }

    /**
     * Gets the preferred width of the button.
     */
    public int getPreferredWidth() {
        return this.getScreen().getWidth() / 7;
    }

    /**
     * Gets the preferred height of the button.
     */
    public int getPreferredHeight() {
        return _labelHeight;
    }

    protected void layout(int width, int height) {
        // Calc width.
        width = getPreferredWidth();

        // Calc height.
        height =getPreferredHeight();

        // Set dimensions.
        setExtent(width, height);
    }

    protected void paint(Graphics graphics) {
//        graphics.setColor(0xDDDDDD);
//        graphics.fillRect( 0, 0, getWidth(), getHeight() );
        int textX, textY, textWidth;
        int w = getWidth();
        if (_isBorder == 0) {
            graphics.drawRect(0, 0, w, getHeight());
        }
        textX = 4;
        textY = 2;
        textWidth = w - 6;
        graphics.drawText(_label, textX, textY, (int) (getStyle() 
                & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK), textWidth);
    }
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。