BlackBerry定制自己喜爱的List Field
2011-01-12 16:57
120 查看
Come from : http://sinfrancis.javaeye.com/blog/508430
===============================================================================
BB上面提供可继承的ListField类,我们可以直接继承此类,然后重写里面的方法,另外继承ListFieldCallBack接口,实现回调。
请看代码:
MyList.java
Java代码
package
com.mdev;
import
net.rim.device.api.ui.ContextMenu;
import
net.rim.device.api.ui.Graphics;
import
net.rim.device.api.ui.component.ListField;
import
net.rim.device.api.ui.component.ListFieldCallback;
/**
*
* @author Sinfrancis wong
* @site: http://mdev.cc
*
*/
public
class
MyList
extends
ListField
implements
ListFieldCallback {
String[] datas = {"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
"7"
,
"8"
,
"9"
,
"10"
,
"11"
,
"12"
,
"13"
,
"14"
,
"15"
,
"16"
,
"17"
,
"18"
,
"19"
,
"20"
};
/**
* 数据总长度
*/
int
dataSize;
/**
* 每一行的高度
*/
int
rowHeight;
public
MyList() {
rowHeight = 40
;
dataSize = datas.length;
/**
* 设置回调函数
*/
setCallback(this
);
/**
* 设置一共有多少行
*/
setSize(dataSize);
/**
* 设置每一行高度
*/
setRowHeight(rowHeight);
}
protected
void
paint(Graphics graphics) {
/**
* 画出背景
*/
graphics.setColor(0x336699
);
graphics.fillRect(0
,
0
, getPreferredWidth(), getPreferredHeight());
graphics.setColor(0x000000
);
int
i =
0
;
while
( i < dataSize){
drawListRow(this
, graphics, i, i*getRowHeight(), getPreferredWidth());
i++;
}
}
protected
void
layout(
int
width,
int
height) {
/**
* 设置整个ListField的总高度和宽度
*/
setExtent(getPreferredWidth(), getPreferredHeight());
}
public
int
getPreferredHeight() {
return
dataSize * rowHeight ;
}
public
int
getPreferredWidth() {
return
360
;
}
public
void
drawListRow(ListField listField, Graphics graphics,
int
index,
int
y,
int
width) {
/**
* 画出文字
*/
graphics.drawText(datas[index], 10
, y);
/**
* 画出下划线
*/
graphics.drawLine(0
, y+rowHeight,
360
, y+rowHeight);
}
public
Object get(ListField listField,
int
index) {
return
datas[index];
}
public
int
getPreferredWidth(ListField listField) {
return
getPreferredWidth();
}
public
int
indexOfList(ListField listField, String prefix,
int
start) {
return
0
;
}
protected
void
makeContextMenu(ContextMenu contextMenu) {
}
}
MyScreen.java
Java代码
package
com.mdev;
import
java.util.Vector;
import
net.rim.device.api.ui.ContextMenu;
import
net.rim.device.api.ui.Manager;
import
net.rim.device.api.ui.component.Menu;
import
net.rim.device.api.ui.container.MainScreen;
import
net.rim.device.api.ui.container.VerticalFieldManager;
/**
*
* @author Sinfrancis wong
* @site: http://mdev.cc
*
*/
public
class
MyScreen
extends
MainScreen {
public
MyScreen() {
MyList m = new
MyList();
/**
* 设置VerticalFieldManager可以滚动
*/
VerticalFieldManager fieldManager = new
VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR|Manager.VERTICAL_SCROLLBAR_MASK){
protected
void
sublayout(
int
maxWidth,
int
maxHeight) {
super
.sublayout(maxWidth, maxHeight);
/**
* 设置Manager的大小为300*200,list里面的数据只在这个范围内滚动,如果注释掉就会显示全屏。
*/
setExtent(300
,
200
);
}
protected
void
makeContextMenu(ContextMenu contextMenu) {
}
protected
void
makeMenu(Menu menu,
int
instance) {
}
};
/**
* 设置外上边距和外左边距
*/
//fieldManager.setPadding(50, 0, 0, 10);
/**
* 设置内上边距和内左边距
*/
fieldManager.setMargin(50
,
0
,
0
,
10
);
//**注意:如果设置外上边距,请将list field的总高度加上您设置的外上边距高度,
//**比如manager外上边距为 50,那么list field的总高度也要加上50,不然无法滚动到底。
//**设置内上边距,list field无需在加上内上边距的高度
//**以上是在非全屏模式下的listfield
fieldManager.add(m);
add(fieldManager);
}
protected
void
makeContextMenu(ContextMenu contextMenu) {
}
protected
void
makeMenu(Menu menu,
int
instance) {
}
}
MyApplication.java
Java代码
package
com.mdev;
import
net.rim.device.api.ui.UiApplication;
/**
*
* @author Sinfrancis wong
* @site: http://mdev.cc
*
*/
public
class
MyApplication
extends
UiApplication {
public
MyApplication() {
MyScreen m = new
MyScreen();
pushScreen(m);
}
public
static
void
main(String[] args) {
MyApplication myApplication = new
MyApplication();
myApplication.enterEventDispatcher();
}
}
所有应该注意的地方都写在注释里面了,请看注释,截图:
===============================================================================
BB上面提供可继承的ListField类,我们可以直接继承此类,然后重写里面的方法,另外继承ListFieldCallBack接口,实现回调。
请看代码:
MyList.java
Java代码
package
com.mdev;
import
net.rim.device.api.ui.ContextMenu;
import
net.rim.device.api.ui.Graphics;
import
net.rim.device.api.ui.component.ListField;
import
net.rim.device.api.ui.component.ListFieldCallback;
/**
*
* @author Sinfrancis wong
* @site: http://mdev.cc
*
*/
public
class
MyList
extends
ListField
implements
ListFieldCallback {
String[] datas = {"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
"7"
,
"8"
,
"9"
,
"10"
,
"11"
,
"12"
,
"13"
,
"14"
,
"15"
,
"16"
,
"17"
,
"18"
,
"19"
,
"20"
};
/**
* 数据总长度
*/
int
dataSize;
/**
* 每一行的高度
*/
int
rowHeight;
public
MyList() {
rowHeight = 40
;
dataSize = datas.length;
/**
* 设置回调函数
*/
setCallback(this
);
/**
* 设置一共有多少行
*/
setSize(dataSize);
/**
* 设置每一行高度
*/
setRowHeight(rowHeight);
}
protected
void
paint(Graphics graphics) {
/**
* 画出背景
*/
graphics.setColor(0x336699
);
graphics.fillRect(0
,
0
, getPreferredWidth(), getPreferredHeight());
graphics.setColor(0x000000
);
int
i =
0
;
while
( i < dataSize){
drawListRow(this
, graphics, i, i*getRowHeight(), getPreferredWidth());
i++;
}
}
protected
void
layout(
int
width,
int
height) {
/**
* 设置整个ListField的总高度和宽度
*/
setExtent(getPreferredWidth(), getPreferredHeight());
}
public
int
getPreferredHeight() {
return
dataSize * rowHeight ;
}
public
int
getPreferredWidth() {
return
360
;
}
public
void
drawListRow(ListField listField, Graphics graphics,
int
index,
int
y,
int
width) {
/**
* 画出文字
*/
graphics.drawText(datas[index], 10
, y);
/**
* 画出下划线
*/
graphics.drawLine(0
, y+rowHeight,
360
, y+rowHeight);
}
public
Object get(ListField listField,
int
index) {
return
datas[index];
}
public
int
getPreferredWidth(ListField listField) {
return
getPreferredWidth();
}
public
int
indexOfList(ListField listField, String prefix,
int
start) {
return
0
;
}
protected
void
makeContextMenu(ContextMenu contextMenu) {
}
}
package com.mdev; import net.rim.device.api.ui.ContextMenu; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.component.ListField; import net.rim.device.api.ui.component.ListFieldCallback; /** * * @author Sinfrancis wong * @site: http://mdev.cc * */ public class MyList extends ListField implements ListFieldCallback { String[] datas = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"}; /** * 数据总长度 */ int dataSize; /** * 每一行的高度 */ int rowHeight; public MyList() { rowHeight = 40; dataSize = datas.length; /** * 设置回调函数 */ setCallback(this); /** * 设置一共有多少行 */ setSize(dataSize); /** * 设置每一行高度 */ setRowHeight(rowHeight); } protected void paint(Graphics graphics) { /** * 画出背景 */ graphics.setColor(0x336699); graphics.fillRect(0, 0, getPreferredWidth(), getPreferredHeight()); graphics.setColor(0x000000); int i = 0; while( i < dataSize){ drawListRow(this, graphics, i, i*getRowHeight(), getPreferredWidth()); i++; } } protected void layout(int width, int height) { /** * 设置整个ListField的总高度和宽度 */ setExtent(getPreferredWidth(), getPreferredHeight()); } public int getPreferredHeight() { return dataSize * rowHeight ; } public int getPreferredWidth() { return 360; } public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width) { /** * 画出文字 */ graphics.drawText(datas[index], 10, y); /** * 画出下划线 */ graphics.drawLine(0, y+rowHeight, 360, y+rowHeight); } public Object get(ListField listField, int index) { return datas[index]; } public int getPreferredWidth(ListField listField) { return getPreferredWidth(); } public int indexOfList(ListField listField, String prefix, int start) { return 0; } protected void makeContextMenu(ContextMenu contextMenu) { } }
MyScreen.java
Java代码
package
com.mdev;
import
java.util.Vector;
import
net.rim.device.api.ui.ContextMenu;
import
net.rim.device.api.ui.Manager;
import
net.rim.device.api.ui.component.Menu;
import
net.rim.device.api.ui.container.MainScreen;
import
net.rim.device.api.ui.container.VerticalFieldManager;
/**
*
* @author Sinfrancis wong
* @site: http://mdev.cc
*
*/
public
class
MyScreen
extends
MainScreen {
public
MyScreen() {
MyList m = new
MyList();
/**
* 设置VerticalFieldManager可以滚动
*/
VerticalFieldManager fieldManager = new
VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR|Manager.VERTICAL_SCROLLBAR_MASK){
protected
void
sublayout(
int
maxWidth,
int
maxHeight) {
super
.sublayout(maxWidth, maxHeight);
/**
* 设置Manager的大小为300*200,list里面的数据只在这个范围内滚动,如果注释掉就会显示全屏。
*/
setExtent(300
,
200
);
}
protected
void
makeContextMenu(ContextMenu contextMenu) {
}
protected
void
makeMenu(Menu menu,
int
instance) {
}
};
/**
* 设置外上边距和外左边距
*/
//fieldManager.setPadding(50, 0, 0, 10);
/**
* 设置内上边距和内左边距
*/
fieldManager.setMargin(50
,
0
,
0
,
10
);
//**注意:如果设置外上边距,请将list field的总高度加上您设置的外上边距高度,
//**比如manager外上边距为 50,那么list field的总高度也要加上50,不然无法滚动到底。
//**设置内上边距,list field无需在加上内上边距的高度
//**以上是在非全屏模式下的listfield
fieldManager.add(m);
add(fieldManager);
}
protected
void
makeContextMenu(ContextMenu contextMenu) {
}
protected
void
makeMenu(Menu menu,
int
instance) {
}
}
package com.mdev; import java.util.Vector; import net.rim.device.api.ui.ContextMenu; import net.rim.device.api.ui.Manager; import net.rim.device.api.ui.component.Menu; import net.rim.device.api.ui.container.MainScreen; import net.rim.device.api.ui.container.VerticalFieldManager; /** * * @author Sinfrancis wong * @site: http://mdev.cc * */ public class MyScreen extends MainScreen { public MyScreen() { MyList m = new MyList(); /** * 设置VerticalFieldManager可以滚动 */ VerticalFieldManager fieldManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR|Manager.VERTICAL_SCROLLBAR_MASK){ protected void sublayout(int maxWidth, int maxHeight) { super.sublayout(maxWidth, maxHeight); /** * 设置Manager的大小为300*200,list里面的数据只在这个范围内滚动,如果注释掉就会显示全屏。 */ setExtent(300, 200); } protected void makeContextMenu(ContextMenu contextMenu) { } protected void makeMenu(Menu menu, int instance) { } }; /** * 设置外上边距和外左边距 */ //fieldManager.setPadding(50, 0, 0, 10); /** * 设置内上边距和内左边距 */ fieldManager.setMargin(50, 0, 0, 10); //**注意:如果设置外上边距,请将list field的总高度加上您设置的外上边距高度, //**比如manager外上边距为 50,那么list field的总高度也要加上50,不然无法滚动到底。 //**设置内上边距,list field无需在加上内上边距的高度 //**以上是在非全屏模式下的listfield fieldManager.add(m); add(fieldManager); } protected void makeContextMenu(ContextMenu contextMenu) { } protected void makeMenu(Menu menu, int instance) { } }
MyApplication.java
Java代码
package
com.mdev;
import
net.rim.device.api.ui.UiApplication;
/**
*
* @author Sinfrancis wong
* @site: http://mdev.cc
*
*/
public
class
MyApplication
extends
UiApplication {
public
MyApplication() {
MyScreen m = new
MyScreen();
pushScreen(m);
}
public
static
void
main(String[] args) {
MyApplication myApplication = new
MyApplication();
myApplication.enterEventDispatcher();
}
}
package com.mdev; import net.rim.device.api.ui.UiApplication; /** * * @author Sinfrancis wong * @site: http://mdev.cc * */ public class MyApplication extends UiApplication { public MyApplication() { MyScreen m = new MyScreen(); pushScreen(m); } public static void main(String[] args) { MyApplication myApplication = new MyApplication(); myApplication.enterEventDispatcher(); } }
所有应该注意的地方都写在注释里面了,请看注释,截图:
![](http://dl.javaeye.com/upload/attachment/163261/5bc44cbe-2ab5-383c-a1af-a90185e4ad3d.jpg)
相关文章推荐
- 吐血推荐:BlackBerry下定制自己的UI空间,包括Button,Field,Manager
- Android中定制自己的Camera
- 8 python 定制自己的类,实现查看属性,按index取值,迭代,切片等个性化类
- Android Volley入门到精通:定制自己的Request(示例,出错代码)
- 怎样使用 App Studio 高速定制你自己的 Universal Windows App
- 定制打包自己的ISO镜像(kickstart)
- Android Volley完全解析(三),定制自己的Request
- 第一部分:定制自己的WINCE6.0
- Android开发中定制自己的日志工具
- Druid简介(Spring Boot + Mybatis + Druid数据源【自己定制】)
- 手把手带你定制自己的nginx rpm包
- 利用javadoc定制自己的接口文档(三)
- kernel自己特别定制
- (六)u-boot2013.01.01 for s5pv210:《精简u-boot文件目录,定制自己的目标板》
- linux裁剪—定制自己所需要的linux
- ASP.NET中定制自己的委托和事件参数类
- 教你如何定制刻录自己喜欢的汽车音乐CD(自定义无损音乐APE格式)
- Android 使用UncaughtExceptionHandler定制自己的错误日志系统
- SuperMap iDesktop扩展开发,如何快速的定制自己的系统