2012-06-12 49 views
0

我需要創建一個自定義的彈出工具提示相似的工作我如何能實現它 我有一個代碼工作,請看看,並告訴我,我需要改進定義彈出像提示工作,沒有時間出來

import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.container.PopupScreen; 
import net.rim.device.api.ui.container.VerticalFieldManager; 


    public class ToolTip extends PopupScreen{ 

     private static VerticalFieldManager vfm; 
     private LabelField lbl; 
     private int xPosition; 
     private int yPosition; 
     private String message; 
     public ToolTip(String message,int xPos,int yPos){ 
      super(vfm); 
      this.xPosition=xPos; 
      this.yPosition=yPos; 
      this.message=message; 
      vfm=new VerticalFieldManager(){ 
       protected void paint(Graphics graphics) { 
        graphics.setColor(0x00FFFFFF); 
        graphics.fillRect(0,0,getWidth(),getHeight()); 
        graphics.setColor(0x00000000); 
        graphics.drawRect(0,0,getWidth(),getHeight()); 
        super.paint(graphics); 
       }  

      }; 

      lbl=new LabelField(message); 
      vfm.add(lbl); 
     } 

     protected void sublayout(int width, int height) { 
      super.sublayout(width, height); 
      setExtent(lbl.getPreferredWidth(), lbl.getPreferredHeight()); 
      setPosition(xPosition, yPosition); 


     } 

    } 

我在super(vfm)處得到NullPointer異常的錯誤,因爲當我如下使用它時,vfm爲空。我怎樣才能優化我的代碼。

ButtonField bf1=new ButtonField("Login"){ 
      protected boolean navigationClick(int status, int time) { 

      UiApplication.getUiApplication().pushScreen(new ToolTip("Hello ", 50, 50)); 
       return super.navigationClick(status, time); 
      } 

     }; 
     add(bf1); 
+0

'我得到一個空字段異常的錯誤 - 在BlackBerry中沒有這樣的例外。也發佈堆棧跟蹤和異常消息。 – Rupak

+0

我也需要重寫vfm的paint()方法 – Yatin

+0

檢查更新後的答案。 – Rupak

回答

1

VerticalFieldManager一個初始化實例的PopupScreensuper(..)。檢查下面的代碼。

class ToolTip extends PopupScreen { 
    private static VerticalFieldManager vfm = new VerticalFieldManager() { 
     protected void paint(Graphics graphics) { 
      graphics.setColor(0x00FFFFFF); 
      graphics.fillRect(0, 0, getWidth(), getHeight()); 
      graphics.setColor(0x00000000); 
      graphics.drawRect(0, 0, getWidth(), getHeight()); 
      super.paint(graphics); 
     } 
    }; 

    // other codes 

    public ToolTip(String message, int xPos, int yPos) { 
     super(vfm); 
     // other codes 
    } 

    protected void sublayout(int width, int height) { 
     // other codes 
    } 
} 


自定義背景支持

class ToolTip extends PopupScreen { 
    private int xPosition; 
    private int yPosition; 

    public ToolTip(String message, int xPos, int yPos) { 
     super(new VerticalFieldManager()); 

     // Define and set background here 
     // Use - BackgroundFactory.createBitmapBackground(bitmap) for bitmap background 
     getDelegate().setBackground(BackgroundFactory.createSolidBackground(Color.RED)); 

     // Add other UI Field here 
     this.xPosition=xPos; 
     this.yPosition=yPos; 

     add(new LabelField(message)); 
     // other codes 
    } 

    // Empty implementation of this will disable default rounded border. 
    protected void applyTheme() { 
    } 

    protected void sublayout(int width, int height) { 
     super.sublayout(width, height); 
     setPosition(xPosition, yPosition); 
    } 
} 

更新代碼,您需要使用getDelegate().setBackground(..)設置背景。請檢查課程BackgroundFactory以創建自定義背景。

+0

它顯示了一些黑色背景我怎樣才能消除我只需要填充的矩形和/或圖像作爲背景,可以作爲參數傳遞給類 - – Yatin

+0

檢查更新的答案。 – Rupak