BlackBerry界面可以分为两类,一类是传统的J2ME界面,以Canvas为基础绘制出所需的界面;一类是BlackBerry风格的界面,UiApplication结合MainScreen。这里介绍BB风格的界面开发。
BB风格界面开发是BlackBerry推荐的界面开发方式,它保持BlackBerry应用程序在整体主题风格下的界面协调,统一,同时也应用了J2SE的界面开发风格,即Container-LayoutManager-BuildIn LayoutManager-Field,Field可以被扩展加入需要的界面元素,这使得高级UI组件也可以开发出各种需要的界面。
这里有一个例子,
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
public class BlackBerry extends UiApplication{
public static void main(String[] args){
//启动应用
BlackBerry bb = new BlackBerry();
bb.enterEventDispatcher();
}
public BlackBerry(){
//创建主页面并显示
MainPageScreen menuScreen = new MainPageScreen(this);
this.pushScreen(menuScreen);
}
}
class MainPageScreen extends MainScreen implements FieldChangeListener{
public static final String TITLE_STRING="欢迎使用黑莓";
//外层的横向Layout
private HorizontalFieldManager parentLayout;
//内层左侧的纵向Layout,显示按钮选项
private VerticalFieldManager menuLayout;
//内层右侧的纵向Layout,显示主题图片
private VerticalFieldManager themeLayout;
//三个按钮选项
private ButtonField introButton;
private ButtonField smartphoneButton;
private ButtonField softwareButton;
//主题图片
private BitmapField imageField;
private Bitmap image;
//UiApplication环境实例
private BlackBerry context;
public MainPageScreen(BlackBerry context){
//对默认Layout进行规划,这里设置为没有横向纵向滚动条
super(Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR);
//对TitleField的设置,文字显示在Field的中央
setTitle(new LabelField(TITLE_STRING,Field.FIELD_VCENTER|Field.FIELD_HCENTER));
//获取UiApplication实例,可以显示其他页面
this.context = context;
//创建外层Layout,设置为使用整个宽度和高度
parentLayout = new HorizontalFieldManager(Field.USE_ALL_WIDTH|Field.USE_ALL_HEIGHT);
//对Layout的背景进行设置,这里用颜色渐变
parentLayout.setBackground(BackgroundFactory.createLinearGradientBackground(Color.CYAN, Color.CYAN, Color.OLIVE, Color.OLIVE));
//创建按钮Layout,规定按钮显示在中央
menuLayout = new VerticalFieldManager(Field.FIELD_HCENTER|Field.FIELD_VCENTER);
//规定按钮Layout背景为全黑色
menuLayout.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
//创建主题图片Layout,沾满全部高度
themeLayout = new VerticalFieldManager(Field.USE_ALL_HEIGHT);
//创建3个按钮选项并注册侦听
introButton = new ButtonField("黑莓概述");
introButton.setChangeListener(this);
smartphoneButton = new ButtonField("智能手机");
smartphoneButton.setChangeListener(this);
softwareButton = new ButtonField("黑莓软件");
softwareButton.setChangeListener(this);
//向按钮Layout加入按钮Field
menuLayout.add(introButton);
menuLayout.add(smartphoneButton);
menuLayout.add(softwareButton);
//向主题Layout加入主题图片
image = Bitmap.getBitmapResource("splash_img.PNG");
imageField = new BitmapField(image);
themeLayout.add(imageField);
//把按钮Layout和主题Layout加入到外层Layout
parentLayout.add(menuLayout);
parentLayout.add(themeLayout);
//最后把外层Layout加到屏幕上
add(parentLayout);
}
public void fieldChanged(Field field, int context) {
// 这里是按钮事件的响应
if (field == introButton){
}
else if (field == smartphoneButton){
}
else if (field == softwareButton){
}
}
public boolean onSavePrompt()
{
// 阻止显示save 对话框
return true;
}
}
这个例子可以运行结果如图: