在BlackBerry中,如何在click事件期间更改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;
}

}

最佳答案

使用 Field BackgroundFactory 的视觉状态指示器,可以为以下视觉状态设置 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);
    

    关于blackberry - 在BlackBerry中的单击事件期间更改ButtonField背景,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11099081/

    10-13 00:23