黑莓开发学习入门系列,自己动手实现一个日历软件(五)
2011-01-20 15:45
791 查看
如何自定义一个自己的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);
}
}
阅读更多
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);
}
}
阅读更多
相关文章推荐
- 黑莓开发学习入门系列,自己动手实现一个日历软件(六)
- 自己动手系列——实现一个简单的LinkedLis
- 自己动手系列——实现一个简单的LinkedLis
- 自己动手系列——实现一个简单的LinkedLis
- windows phone开发学习--自己实现一个Gallery control
- 自己动手系列——实现一个简单的ArrayList
- 自己动手系列——实现一个简单的LinkedLis
- 自己动手系列——实现一个简单的LinkedLis
- 自己动手系列——实现一个简单的LinkedList
- OSGi 入门开发学习资料必读系列
- 软件开发入门学习的个人看法
- 【转载】黑莓开发学习(入门教程)05-再说开发环境和入门阶段的一些问题
- OpenCV(Emgu)入门系列(9):在C#中,使用Emgu+PictureBox实现一个简易的视频播放器
- 抛砖引玉式炫技:日历图标的纯CSS实现【前端开发技能必备系列】
- ajax实时任务提示功能的实现 -- vb2005xu自己动手系列(1)
- 黑莓开发新手入门教学帖,如何制作一个能控制LED颜色的程序(四)
- 实现自己的软件做的编辑保存成特定后缀,可以用自己开发的软件打开
- 1:Win8 Metro应用开发的第一次学习1:创建一个 Metro 项目——Win8Y团队手把手教您Win8 Metro应用开发系列
- 菜鸟的Hadoop学习之路系列<一>:自己动手搭建Hadoop集群
- 线程、消息-Android入门开发系列学习之 Handler使用-by小雨