设置重点放在黑莓自定义按钮字段

时间:2012-05-23 12:08:42

标签: blackberry

我是黑莓新手,

我正在使用自定义图像按钮字段,如标签,我有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());

    }
}
}

1 个答案:

答案 0 :(得分:2)

我在这里得到了解决上述问题的解决方案代码,

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