在BlackBerry中单击事件期间更改ButtonField背景

时间:2012-06-19 10:26:57

标签: blackberry blackberry-eclipse-plugin

在BlackBerry中,如何在点击事件期间更改ButtonField背景色?例如,对于长按,背景颜色需要改变。对我而言,它的默认颜色为蓝色。如何改变?

这是我们的定制按钮字段。但它显示按钮单击事件的默认蓝色。

public class CustomButtonField extends ButtonField implements GlobalConstant {
int mHeight;
int mWidth;
public final static int DEFAULT_BACKGROUND_COLOR_NORMAL = 0x167c9c;
public final static int DEFAULT_BACKGROUND_COLOR_ON_FOCUS = 0x188118;
private int backgroundColorNormal = DEFAULT_BACKGROUND_COLOR_NORMAL;
private int backgroundColorOnFocus = DEFAULT_BACKGROUND_COLOR_ON_FOCUS;
private Background noraml_bg;
private Background focus_bg;
private boolean isFocusable;
private boolean isround_button = false;


public CustomButtonField(int height, int width, String label) {
    super(label, CONSUME_CLICK);

    noraml_bg = menuButton_bgNormal;
    focus_bg = menuButton_bgFocus;

    mHeight = height;
    mWidth = width;
    this.isFocusable = true;
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE,
            BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));

}

public CustomButtonField(int height, int width, String label, boolean isround_button) {
    super(label, CONSUME_CLICK);

    this.isround_button = isround_button;
    noraml_bg = roundButton_bgNormal;
    focus_bg = roundButton_bgFocus;
    mHeight = height;
    mWidth = width;
    this.isFocusable = true;

    XYEdges padding = new XYEdges(1,1,1,1);
    XYEdges color = new XYEdges (Color.BLACK,Color.BLACK,Color.BLACK,Color.BLACK);
    int lineStyle = Border.STYLE_SOLID;

   Border roundedBorder = BorderFactory.createSimpleBorder(padding, color, lineStyle);
    setBorder(roundedBorder);

}

/*
 * (non-Javadoc)
 * 
 * @see net.rim.device.api.ui.component.ButtonField#getPreferredHeight()
 */
public int getPreferredHeight() {
    return mHeight;
}

/*
 * (non-Javadoc)
 * 
 * @see net.rim.device.api.ui.component.ButtonField#getPreferredWidth()
 */
public int getPreferredWidth() {
    return mWidth;
}

/*
 * (non-Javadoc)
 * 
 * @see net.rim.device.api.ui.component.ButtonField#layout(int, int)
 */
protected void layout(int width, int height) {
    super.layout(mWidth, mHeight);
    setExtent(mWidth, mHeight);
}

/*
 * (non-Javadoc)
 * 
 * @see
 * net.rim.device.api.ui.component.ButtonField#paint(net.rim.device.api.
 * ui.Graphics)
 */
protected void paint(Graphics graphics) {

    String label = getLabel();
    int x = (getPreferredWidth() - getFont().getAdvance(label)) >> 1;
    int y = (getPreferredHeight() - getFont().getHeight()) >> 1;
    if (isFocus() == false) {
        this.setBackground(noraml_bg);
        if(isround_button){
            graphics.setColor(0x666666);
        }else{
            graphics.setColor(Color.WHITE);
        }

        graphics.drawText(label, x, y);
    } else {
        this.setBackground(focus_bg);
        graphics.setColor(Color.WHITE);

        graphics.drawText(label, x, y);
    }
}

protected void drawFocus(Graphics graphics, boolean on) {
    if (on) {
        graphics.setColor(backgroundColorOnFocus);
    } else {
        graphics.setColor(backgroundColorNormal);
    }
}

public boolean isFocusable() {
    return isFocusable;
}

}

2 个答案:

答案 0 :(得分:8)

使用FieldBackgroundFactory的视觉状态指示符,您可以为后续视觉状态设置Background

  • VISUAL_STATE_ACTIVE - 主动视觉状态。用户正在与该字段进行交互。
  • VISUAL_STATE_DISABLED - 已停用的视觉状态。没有可能与该领域的互动。
  • VISUAL_STATE_DISABLED_FOCUS - 已停用,但重点是视觉状态。该字段突出显示,但该字段没有其他可能的交互。
  • VISUAL_STATE_FOCUS - 关注视觉状态。该字段具有焦点(突出显示)。
  • VISUAL_STATE_NORMAL - 正常的视觉状态。目前没有与该领域的互动。


请查看以下代码段:

ButtonField bfTest = new ButtonField("Button Field");

Background commonBgOne = BackgroundFactory.createSolidBackground(Color.RED);
Background commonBgTwo = BackgroundFactory.createSolidBackground(Color.GREEN);

bfTest.setBackground(VISUAL_STATE_ACTIVE, commonBgOne);
bfTest.setBackground(VISUAL_STATE_DISABLED, commonBgTwo);
bfTest.setBackground(VISUAL_STATE_DISABLED_FOCUS, commonBgTwo);
bfTest.setBackground(VISUAL_STATE_FOCUS, commonBgOne);
bfTest.setBackground(VISUAL_STATE_NORMAL, commonBgTwo);


取消默认边框

Border commonBorder = BorderFactory.createSimpleBorder(new XYEdges());

bfTest.setBorder(VISUAL_STATE_ACTIVE, commonBorder);
bfTest.setBorder(VISUAL_STATE_DISABLED, commonBorder);
bfTest.setBorder(VISUAL_STATE_DISABLED_FOCUS, commonBorder);
bfTest.setBorder(VISUAL_STATE_FOCUS, commonBorder);
bfTest.setBorder(VISUAL_STATE_NORMAL, commonBorder);

答案 1 :(得分:2)

您是否尝试过使用按钮的setBackground属性?