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

如何自定义一个自己的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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值