2013-03-15 78 views
0

水平地,我想顯示兩個自定義按鈕,並在它們之間顯示BlackBerry應用程序中的標籤字段。我看着「BlackBerry HorizontalFieldManager alignment」,但沒有取得任何成功。 這是我想要在BlackBerry中創建的屏幕截圖。 enter image description hereHorizo​​ntalFieldManager在BlackBerry中對齊

這裏是我此屏幕創建的代碼:

package mypackage; 

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.Font; 
import net.rim.device.api.ui.Manager; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.XYEdges; 
import net.rim.device.api.ui.component.BasicEditField; 
import net.rim.device.api.ui.component.BitmapField; 
import net.rim.device.api.ui.component.ButtonField; 
import net.rim.device.api.ui.component.Dialog; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.component.PasswordEditField; 
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.Background; 
import net.rim.device.api.ui.decor.BackgroundFactory; 
import net.rim.device.api.ui.decor.Border; 
import net.rim.device.api.ui.decor.BorderFactory; 

    /** 
* A class extending the MainScreen class, which provides default standard * behavior for BlackBerry GUI applications. */ public final class HelloBlackBerryScreen extends MainScreen { BasicEditField username; PasswordEditField password; 

/** 
* Creates a new MyScreen object 
*/ public HelloBlackBerryScreen() { // Set the linear background. this.getMainManager().setBackground(BackgroundFactory.createLinearGradientBackground(0x91e7ff,0x0099CCFF,0x00336699,0x91e7ff)); 
// SET HEADER OF SCREEN VerticalFieldManager vfm = new 
VerticalFieldManager(Manager.USE_ALL_WIDTH); BitmapField header = new 
BitmapField(Bitmap.getBitmapResource("header.png"),FIELD_HCENTER); 

HorizontalFieldManager hfm = new 
HorizontalFieldManager(Field.FIELD_VCENTER |Manager.USE_ALL_WIDTH); 
setTitle(header); 

hfm.add(vfm); add(hfm); 

//SET Container 

LabelField usernameTxt = new LabelField("Username:",LabelField.FIELD_TOP); 

usernameTxt.setFont(Font.getDefault().derive(Font.PLAIN, 30)); 

usernameTxt.setMargin(20, 0, 0, 160); 

LabelField passwordTxt = new LabelField("Password :",LabelField.FIELD_BOTTOM); 

    passwordTxt.setFont(Font.getDefault().derive(Font.PLAIN, 30)); 

    passwordTxt.setMargin(10, 0, 0, 160); 

    username = new BasicEditField(BasicEditField.FIELD_BOTTOM);username.setMargin(10, 110, 0, 160); //username.setMaxSize(getHeight()); 
    username.setBorder(BorderFactory.createRoundedBorder(new XYEdges(3, 3,3, 3), 0x999999, Border.STYLE_FILLED)); 
    username.setBackground(BackgroundFactory.createSolidBackground(0xe0e0e0)); 
    password = new PasswordEditField(); password.setMargin(10, 110, 0,160); password.setBorder(BorderFactory.createRoundedBorder(new 
     XYEdges(3, 3, 3, 3), 0x999999, Border.STYLE_FILLED)); 
    password.setBackground(BackgroundFactory.createSolidBackground(0xe0e0e0)); 

    ButtonField loginBtn = new ButtonField(" Log-In ", ButtonField.CONSUME_CLICK); loginBtn.setMargin(30, 0, 0,240); 
    ButtonField recoveryBtn = new ButtonField("Forget Password", ButtonField.CONSUME_CLICK); recoveryBtn.setMargin(10, 0, 0,200); 


    add(usernameTxt); add(username); add(passwordTxt); add(password); 
    add(loginBtn); add(recoveryBtn); 
    loginBtn.setChangeListener(btnlistener); } FieldChangeListener 
    btnlistener = new FieldChangeListener() { 

     public void fieldChanged(Field field, int context) { 
      //Open a new screen 
      String uname = username.getText(); 
      String pwd =password.getText(); 

     //If there is no input if (uname.length() == 0 || pwd.length()==0) 
      Dialog.alert("One of the textfield is empty!");  
      else if 
      (uname.equals("user") && pwd.equals("admin")) 
     UiApplication.getUiApplication().pushScreen(newwelcome());   
      //Open a 
      new Screen else 
       Dialog.alert("Username or password not found!");  
      } 

     }; 

     FieldChangeListener btnlistener2 = new FieldChangeListener() { 

     public void fieldChanged(Field field, int context) {  
      screen if variables are not empty 




      [1]: http://i.stack.imgur.com/PlDRu.png 
+0

請使用** {} **按鈕在問題編輯器中格式化您的代碼。您還需要從編輯器(IDE)中將其粘貼,該編輯器將空格放入縮進中,而不是製表符。此代碼不可讀。 – Nate 2013-03-15 22:07:15

回答

1

對於您需要使用自定義的經理像這樣。

主要現場經理,該屏幕

VerticalFieldManager vfm = new VerticalFieldManager(VerticalFieldManager.USE_ALL_HEIGHT 
       |VerticalFieldManager.USE_ALL_WIDTH|VerticalFieldManager.FIELD_VCENTER|VerticalFieldManager.FIELD_HCENTER){ 
      //Override the paint method to draw the background image. 
      public void paint(Graphics graphics){ 
       //Draw the background image and then call super.paint 
       //to paint the rest of the screen. 
        graphics.setBackgroundColor(Color.DEEPSKYBLUE); 
       graphics.clear(); 
       super.paint(graphics); 
      } 
     }; 

     add(vfm); 

,並添加自定義的經理這樣

//配售回來,在家裏特定位置

HorizontalFieldManager customManager = new HorizontalFieldManager(HorizontalFieldManager 
       .USE_ALL_WIDTH) 
     { 
      //Applying background color for that Manager 
      public void paint(Graphics graphics) 
      { 
       graphics.setBackgroundColor(Color.DEEPSKYBLUE);//blue 
       graphics.clear(); 
       super.paint(graphics); 
      } 
      //Placing the Fields 
      protected void sublayout(int width, int height) { 

       setPositionChild(
         getField(0), 
         0, 
         0); 
       layoutChild(
         getField(0), 
         getField(0).getPreferredWidth(), 
         getField(0).getPreferredHeight()); 

       setPositionChild(
         getField(1), 
         Display.getWidth()/2 - getField(1).getPreferredWidth()/2, 
         0); 
       layoutChild(
         getField(1), 
         getField(1).getPreferredWidth(), 
         getField(1).getPreferredHeight());  

       setPositionChild(
         getField(2), 
         Display.getWidth() - getField(2).getPreferredWidth(), 
         0); 
       layoutChild(
         getField(2), 
         getField(2).getPreferredWidth(), 
         getField(2).getPreferredHeight());  

       setExtent(width, 50); 
      }  
     }; 

圖標和標題//顯示返回圖標

final Bitmap bmp1 = Bitmap.getBitmapResource("back.png"); 
    BitmapField bmpfield1 = new BitmapField(bmp1,BitmapField.FOCUSABLE|BitmapField.FIELD_LEFT); 

//要顯示主頁圖標

final Bitmap bmp2 = Bitmap.getBitmapResource("home.png"); 
BitmapField bmpfield2 = new BitmapField(bmp2,BitmapField.FOCUSABLE |BitmapField.FIELD_RIGHT); 

//要顯示標題

LabelField lbl = new LabelField("Login",LabelField.FIELD_VCENTER); 

//添加字段

customManager.add(bmpfield1); 
customManager.add(lbl); 
customManager.add(bmpfield2); 
vfm. add(customManager); 
+0

我現在面臨的問題是我想爲屏幕做這個「標題」修復。像它應該保持修復屏幕的頂部。要實現這個任務?請幫助我「用戶1213202」,並感謝您的回覆 – user2174312 2013-03-19 13:53:09

+0

這將只是固定的。我沒有得到正是你問的。如果你想在任何其他字段後,你可以添加它的自定義管理器。 – user1213202 2013-03-20 03:44:19

1

如果你想在修復您的頭頂部,因爲我從你的意見了解你可以做什麼是創建一個HorizontalFieldManager並添加你的背部,標題和主頁按鈕,然後你可以設置該hor izontal現場經理在您的標題使用setTitle(hfm)。同樣,如果您還需要修復頁腳,則可以使用setStatus(hfm)

相關問題