您的位置:首页 > 其它

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) {

}

}

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();

}
}


所有应该注意的地方都写在注释里面了,请看注释,截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: