2012-05-23 29 views
0

我在黑莓新,組專注於黑莓自定義按鈕字段

我使用自定義圖像按鈕字段,如標籤,我有3個圖像按鈕(狀突片)和3個screens.All 3個屏幕具有3圖像按鈕,如果我點擊2圖像按鈕導航到下一個屏幕。它注重第一圖像按鈕只(第一個選項卡),但我想,這將集中在第二圖像按鈕,任何一個能幫助我在這裏我的代碼,

ImageButtonField.java

public class ImageButtonField extends ButtonField { 
Bitmap mNormal; 
Bitmap mFocused; 
Bitmap mActive; 

String text; 
int mWidth; 
int mHeight; 

public ImageButtonField(Bitmap normalImage, Bitmap focusedImage, 
    Bitmap activeImage,String text) { 
    super(CONSUME_CLICK); 
    this.text=text; 
    mNormal = normalImage; 
    mFocused = focusedImage; 
    mActive = activeImage; 
    mWidth = mNormal.getWidth(); 
    mHeight = mNormal.getHeight(); 
    setMargin(0, 0, 0, 0); 
    setPadding(0, 0, 0, 0);  
    setBorder(BorderFactory 
       .createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    setBorder(VISUAL_STATE_ACTIVE, BorderFactory 
      .createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
} 

protected void paint(Graphics graphics) { 
    Bitmap bitmap = null; 
    switch (getVisualState()) { 
    case VISUAL_STATE_NORMAL: 
     bitmap = mNormal; 
     break; 
    case VISUAL_STATE_FOCUS: 
     bitmap = mFocused; 
     break; 
    case VISUAL_STATE_ACTIVE: 
     bitmap = mActive; 
     break; 
    default: 
     bitmap = mNormal; 
    } 
    graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(), 
      bitmap, 0, 0); 
    if(text!=null) 
    { 
     graphics.setColor(Color.WHITE); 
    graphics.setFont(getFont().derive(Font.BOLD, 28)); 
    // graphics. 
    graphics.drawText(text,50, 30); 
    } 


} 

public int getPreferredWidth() { 
    return mWidth; 
} 

public int getPreferredHeight() { 
    return mHeight; 
} 

protected void layout(int width, int height) { 
    setExtent(mWidth, mHeight); 

} 
} 

HomeScreen.java

public final class HomeScreen extends MainScreen implements FieldChangeListener { 

private Bitmap homeImageOn; 
private Bitmap activateImageOn; 
private Bitmap socialImageOn; 
private Bitmap homeImageOff; 
private Bitmap activateImageOff; 
private Bitmap socialImageOff; 
private Bitmap logo; 
private Bitmap driveNowTextBgImage; 
private Bitmap webLink1Image; 
private Bitmap webLink2Image; 

ImageButtonField homeBitmapImageResource; 
ImageButtonField activateBitmapImageResource; 
ImageButtonField socialBitmapImageResource; 
ImageButtonField logoBitmapResource; 
ImageButtonField driveNowBgImageButtonField; 

BitmapField webLink1BitmapResorce; 
BitmapField webLink2BitmapResorce; 

/** 
* Creates a new HomeScreen object 
*/ 
public HomeScreen() { 
    // Set the displayed title of the screen 
    HorizontalFieldManager tabHorizantalManager = new HorizontalFieldManager(); 
    VerticalFieldManager contentVerticalManager = new VerticalFieldManager(); 



    homeImageOn = Bitmap.getBitmapResource("home-on.png"); 
    activateImageOff = Bitmap.getBitmapResource("activate-off.png"); 
    socialImageOff = Bitmap.getBitmapResource("social-off.png"); 
    homeImageOff = Bitmap.getBitmapResource("home-off.png"); 
    activateImageOn = Bitmap.getBitmapResource("activate-on.png"); 
    socialImageOn = Bitmap.getBitmapResource("social-on.png"); 


    Bitmap bitmap = Bitmap.getBitmapResource("bg.png"); 
    getMainManager().setBackground(
      BackgroundFactory.createBitmapBackground(bitmap)); 

    homeBitmapImageResource = new ImageButtonField(homeImageOff, 
      homeImageOn, homeImageOff, null); 
    activateBitmapImageResource = new ImageButtonField(activateImageOff, 
      activateImageOn, activateImageOff, null); 
    socialBitmapImageResource = new ImageButtonField(socialImageOff, 
      socialImageOn, socialImageOff, null); 




    tabHorizantalManager.add(homeBitmapImageResource); 
    tabHorizantalManager.add(activateBitmapImageResource); 
    tabHorizantalManager.add(socialBitmapImageResource); 



    add(tabHorizantalManager); 


    socialBitmapImageResource.setChangeListener(this); 
    activateBitmapImageResource.setChangeListener(this); 
    homeBitmapImageResource.setChangeListener(this); 

} 

public void fieldChanged(Field field, int context) { 
    // TODO Auto-generated method stub 

    if (field.equals(homeBitmapImageResource)) { 
     UiApplication.getUiApplication().pushScreen(new HomeScreen()); 
    } 
    if (field.equals(activateBitmapImageResource)) { 

     ActivateScreen activateScreen = new ActivateScreen(); 
     UiApplication.getUiApplication().pushScreen(activateScreen);    
     System.out.println("Button pressed: "); 
    } 
    if (field.equals(socialBitmapImageResource)) { 
     UiApplication.getUiApplication().pushScreen(new SocialScreen()); 

    } 
} 
} 
+0

有你看[在此blackbe rry示例](http://supportforums.blackberry.com/t5/Java-Development/Create-tabbed-view-screens/ta-p/444969)?我知道它使用'LabelFields'而不是你的自定義圖像按鈕字段,但它可能是一個很好的開始。你可以嘗試用一個更簡單的內置字段替換你的'ImageButtonField'(像一個LabelField),並確保你可以先完成這個工作。然後,添加你的ImageButtonField。 – Nate

+0

我得到了上述問題在這裏我的解決方案代碼解決方案,保護無效onDisplay(){\t \t \t \t \t socialImageButtonField.setFocus(); \t} – selva

回答

2

我得到了上述問題在這裏我的解決方案代碼解決方案,

protected void onDisplay(){ 
socialImageButtonField.setFocus(); 
}