0

我是黑莓新手,

我正在使用自定义图像按钮字段,如选项卡,我有 3 个图像按钮(如选项卡)和 3 个屏幕。所有 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());

    }
}
}
4

1 回答 1

2

我在我的解决方案代码中找到了上述问题的解决方案,

protected void onDisplay(){
socialImageButtonField.setFocus(); 
} 
于 2012-06-06T11:57:59.017 回答