var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-20738293-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script')"/>
jutleo
欢迎走进有风的地方~~
posts - 63,  comments - 279,  trackbacks - 0
如何自定义一个自己的UI组件?
创建一个自定义UI,需要继承自现有的组件或者直接继承Field类,通常我们必须实现layout()和paint()方法用于显示想要的UI界面。
1. layout()方法可在手机屏幕上实现一个具有宽度和高度的区域,调用setExtent(width, height);实现。getPreferredWidth()、getPreferredHeight()告诉容器出现在屏幕上合适的高度和宽度
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
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(
00, 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
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(
00, w, getHeight());
        }
        textX 
= 4;
        textY 
= 2;
        textWidth 
= w - 6;
        graphics.drawText(_label, textX, textY, (
int) (getStyle() 
                
& DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK), textWidth);
    }
}
posted on 2011-01-20 15:45 凌晨风 阅读(1809) 评论(1)  编辑  收藏 所属分类: BlackBerry

FeedBack:
# re: 黑莓开发学习入门系列,自己动手实现一个日历软件(五)
2011-01-20 15:56 | 蒙奇奇
沙发观看  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

<2011年1月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(11)

我参与的团队

随笔分类

随笔档案

文章分类

文章档案

新闻分类

新闻档案

收藏夹

围脖

最新随笔

搜索

  •  

最新评论

阅读排行榜

评论排行榜