问题
I am using bitmap button field in advanced ui examples. By default no method is working for disabling the button in 5.0 jre, so i have added the below code for disabling and then disabling functionality of the button is working but setchangelistener is not working that is my problem .. here is my code that i added for disabling the button..please check. do i need to change anything in invoke action method?
public boolean isDisable() {
return isDisable;
}
public void setDisable(boolean isDisable) {
this.isDisable = isDisable;
invalidate();
}
public boolean isFocusable() {
return isFocusable && !isDisable;
}
public void setFocusable(boolean isFocusable) {
this.isFocusable = isFocusable;
}
protected boolean invokeAction(int action) {
if (!isDisable){
fieldChangeNotify(0);
}
return true;
}
public boolean setEnabled() {
return false;
}
回答1:
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.
来源:https://stackoverflow.com/questions/11449613/how-to-disable-the-button-using-bitmapbuttonfield-in-advanced-ui-examples-in-bla