1

我在高级 ui 示例中使用位图按钮字段。默认情况下,没有任何方法可用于禁用 5.0 jre 中的按钮,因此我添加了以下代码以禁用然后禁用按钮的功能正在工作,但 setchangelistener 不起作用这是我的问题.. 这是我添加的代码禁用按钮..请检查。我需要更改调用操作方法中的任何内容吗?

 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;
 }
4

1 回答 1

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.

于 2012-07-15T01:16:12.943 回答