黑莓界面开发实例

BlackBerry界面可以分为两类,一类是传统的J2ME界面,以Canvas为基础绘制出所需的界面;一类是BlackBerry风格的界面,UiApplication结合MainScreen。这里介绍BB风格的界面开发。

BB风格界面开发是BlackBerry推荐的界面开发方式,它保持BlackBerry应用程序在整体主题风格下的界面协调,统一,同时也应用了J2SE的界面开发风格,即Container-LayoutManager-BuildIn LayoutManager-FieldField可以被扩展加入需要的界面元素,这使得高级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;
  }
}

这个例子可以运行结果如图:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值