Here is a discussion on the BlackBerry forums about this.
What I've sometimes done is actually make use of the isEditable()
property on Field
objects, since editability and being enabled are somewhat similar concepts. If you really want to keep the separate isDisabled()
code, that's fine. Just substitute that below where I use isEditable()
(remembering to reverse the boolean ... that's one reason to always program in the affirmative ... make your method isEnabled()
instead of isDisabled()
).
So, instead of any of the code you posted above, I would just add this code to either BitmapButtonField
, or BaseButtonField
:
public boolean isFocusable() {
return isEditable() && super.isFocusable();
}
and this in BitmapButtonField
:
protected void paint( Graphics g ) {
int oldAlpha = g.getGlobalAlpha();
int index = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? FOCUS : NORMAL;
if (!isEditable()) {
g.setGlobalAlpha(100); // alpha is 0 to 255, so this is 100/255
}
g.drawBitmap( 0, 0, _bitmaps[index].getWidth(), _bitmaps[index].getHeight(), _bitmaps[index], 0, 0 );
g.setGlobalAlpha(oldAlpha);
}
And then, I can setup a change listener, or disable the button, like this in my manager class:
BitmapButtonField btn =
new BitmapButtonField(Bitmap.getBitmapResource("button.png"),
Bitmap.getBitmapResource("button-lit.png"));
btn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.alert("Button clicked!");
}
});
btn.setEditable(false); // this disables the button
add(btn);
But, understand, that if you disable a button, that means your change listener won't get called. That's kind of how it's supposed to work. The change listener is only called if the button's enabled and therefore clickable.
Also, note that in order to make the button look different when disabled (not editable), I override paint()
to set a different alpha value when the button is disabled. You didn't mention that, so if you don't like it, you can certainly take it out.