做J2ME开发一般都会遇到九宫格问题,现将我最近写的一小段例子分享一下。
MainUIScreen :负责九宫格布局的
/**
*
*/
package com.blackberry.ui;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.FlowFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import com.blackberry.ui.component.BitmapLabelField;
import com.blackberry.ui.container.ForegroundManager;
/**
* @author zhqguo
* @date 2011-4-29
* @version 1.0
*
*/
public class MainUIScreen extends MainScreen {
private ForegroundManager _foreground;
private FlowFieldManager _manager;
public MainUIScreen() {
super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT | USE_ALL_WIDTH);
setTitle("黑莓九宫格设计");
_foreground = new ForegroundManager();
_manager = new FlowFieldManager();
BitmapLabelField bf1 = new BitmapLabelField("测试模块", Bitmap
.getBitmapResource("png-0015.png"));
bf1.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.alert("Hello");
}
});
BitmapLabelField bf2 = new BitmapLabelField("测试模块", Bitmap
.getBitmapResource("png-0015.png"));
bf2.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.alert("Hello");
}
});
BitmapLabelField bf3 = new BitmapLabelField("测试模块", Bitmap
.getBitmapResource("png-0015.png"));
BitmapLabelField bf4 = new BitmapLabelField("测试模块", Bitmap
.getBitmapResource("png-0015.png"));
BitmapLabelField bf5 = new BitmapLabelField("测试模块", Bitmap
.getBitmapResource("png-0015.png"));
BitmapLabelField bf6 = new BitmapLabelField("测试模块", Bitmap
.getBitmapResource("png-0015.png"));
BitmapLabelField bf7 = new BitmapLabelField("测试模块", Bitmap
.getBitmapResource("png-0015.png"));
BitmapLabelField bf8 = new BitmapLabelField("测试模块", Bitmap
.getBitmapResource("png-0015.png"));
BitmapLabelField bf9 = new BitmapLabelField("测试模块", Bitmap
.getBitmapResource("png-0015.png"));
setMargin(bf1);
setMargin(bf2);
setMargin(bf3);
setMargin(bf4);
setMargin(bf5);
setMargin(bf6);
setMargin(bf7);
setMargin(bf8);
setMargin(bf9);
_manager.add(bf1);
_manager.add(bf2);
_manager.add(bf3);
_manager.add(bf4);
_manager.add(bf5);
_manager.add(bf6);
_manager.add(bf7);
_manager.add(bf8);
_manager.add(bf9);
_foreground.add(_manager);
add(_foreground);
}
/**
* 设置图标显示位置
*
* @param bf
*/
private void setMargin(BitmapLabelField bf) {
bf.setMargin(10, 60, 10, 60);
}
}
BitmapLabelField :设置图标和文字显示样式
package com.blackberry.ui.component;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
/**
*
* @author zhqguo
* @date 2011-4-28
* @version 1.0
*
*/
public class BitmapLabelField extends Field {
private String _title;
private Bitmap _bitmap;
public BitmapLabelField(String title, Bitmap bitmap) {
this(title, bitmap, 0);
}
public BitmapLabelField(String title, Bitmap bitmap, long style) {
super(Field.FOCUSABLE | style);
_title = title;
_bitmap = bitmap;
}
/**
* 设置显示区域大小
*/
protected void layout(int width, int height) {
setExtent(80, 90);
}
/**
* 设置图标显示位置
*/
protected void paint(Graphics g) {
g.drawBitmap(8, 0, _bitmap.getWidth(), _bitmap.getHeight(), _bitmap, 0,
0);
g.setFont(g.getFont().derive(Font.BOLD, 21));
g.drawText(_title, 0, _bitmap.getHeight() + 4, g.HCENTER);
}
/**
* 设置域选中时的样式
*/
protected void drawFocus(Graphics g, boolean on) {
g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);
paintBackground(g);
paint(g);
}
/**
* 设置域的背景
*/
protected void paintBackground(Graphics g) {
if (!isFocus()) {
super.paintBackground(g);
return;
}
int oldColour = g.getColor();
try {
XYRect rt = getExtent();
XYRect rect = new XYRect(0, 0, rt.width, rt.height);
SfBackgroundFactory.createLinearGradientRoundBackground(Color.BLUE,
Color.BLUE, Color.LIGHTSKYBLUE, Color.LIGHTSKYBLUE, 20, 20)
.draw(g, rect);
} finally {
g.setColor(oldColour);
}
}
protected boolean keyChar(char character, int status, int time) {
if (character == Characters.ENTER) {
clickButton();
return true;
}
return super.keyChar(character, status, time);
}
protected boolean navigationClick(int status, int time) {
clickButton();
return true;
}
protected boolean trackwheelClick(int status, int time) {
clickButton();
return true;
}
protected boolean invokeAction(int action) {
switch (action) {
case ACTION_INVOKE: {
clickButton();
return true;
}
}
return super.invokeAction(action);
}
public void setDirty(boolean dirty) {
// We never want to be dirty or muddy
}
public void setMuddy(boolean muddy) {
// We never want to be dirty or muddy
}
/**
* A public way to click this button
*/
public void clickButton() {
fieldChangeNotify(0);
}
}
最终显示效果如下:
原文链接:
http://52123.iteye.com/blog/1025343
分享到:
相关推荐
九宫格黑莓键盘锁 让你的黑莓手机很炫 适用手9530等95系列手机
适合新手的九宫格设计,符合让新手了解其功能实现.
2.在第三方程序中 (1)进入选项->屏幕/键盘设置任一快捷键到百度记事本(BaiduNotepad)。 (2)在需要输入处按下上一步设置好的快捷键。 (3)输入完中文后退出,在需要的地方粘贴即可。 三.目前的版本支持以下...
本文的组成部分是以以一个传统移动应用的九宫格程序的UI 设计和实现为范例,讨论并着重介绍 了在BlackBerry 平台上设计UI 需要考虑和借用的技术,更好的包装BlackBerry 的应用 通过对UI 应用的讨论,本文也涉及到了...
2.黑莓MIDlet开发指南 [文档说明]BlackBerry MIDlet Development Guide V4.0 介绍如何使用黑莓JDE开发用于黑莓手机的标准MIDlet程序。 文档名:BlackBerry_MIDlet_Developer_Guide.pdf 3.黑莓桌面类API参考 ...
在入门篇中,我们讨论了如何应用Blackberry 提供的Widget API 来进行开发,从如下链接中, http://www.blackberry.com/developers/docs/widgetapi/ 可以找到BlackBerry 能提供的所有Widget API 列表。然后,对于很多...
Learn BlackBerry Games Development Paperback: 504 pages Publisher: Apress; 1 edition (March 30, 2010) Language: English ISBN-10: 1430227184 ISBN-13: 978-1430227182 Format: PDF You can buy this book: ...
JDE是黑莓的java开发环境 要做黑莓开发,首先就得看懂这个文档
黑莓开发的入门教程,提供了黑莓开发的案例教程,英文原版,有想学习黑莓开发的可以看看
BlackBerry_Widget_开发中级篇
黑莓 blackberry8700g 游戏 软件 经典集合
在进行Widget 开发之前,开发人员需要下载相应的开发工具,BlackBerry Widget SDK, 这个工 具提供了一些命令行工具可以用来打包编译Widget 工程。BlackBerry 也提供了为Eclipse 设 计的Web plug-in,通过这个plug-in,...
RIM (Research in Motion) 公司为BlackBerry 开发提供了多种开发平台,不同平台的开发 人员可以选择自己熟悉的开发工具开发BlackBerry 应用程序。本节以BlackBerry JDE Plug-In for Eclipse 为例描述了BlackBerry ...
Advanced BlackBerry Development 350 pages | Apress (December 10, 2009) | ISBN: 1430226560 | PDF | 5.6 MB BlackBerry devices and applications are selling by the millions. As a BlackBerry developer, ...
BlackBerry 应用开发指南 卷I-基础部分 卷II-高级部分
BlackBerry开发平台及入门介绍 轻松入门
黑莓(BlackBerry)开发从入门到精通,一线黑莓应用开发人员经验集萃.· 首本全面介绍黑莓开发的中文技术书籍· 一线黑莓应用开发人员经验集萃· RIM中国区总裁鼎力推荐
RIM官方开发期刊,一年两季 此次发布的是2004年的两季 会更新到2008年。 Tag: 黑莓 BlackBerry 开发期刊
这是blackberry平台下的一个带农历的日历源码,当时学习黑莓开发时写的 www.blogjava.net/bulktree有黑莓开发的一系列文章,源码在此