黑莓界面開發實例

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;
  }
}

這個例子可以運行結果如圖:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章