如何在黑莓高级UI的例子使用bitmapbuttonfield禁用按钮 [英] how to disable the button using bitmapbuttonfield in advanced ui examples in blackberry

查看:124
本文介绍了如何在黑莓高级UI的例子使用bitmapbuttonfield禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用先进的用户界面的例子图按钮场。
默认情况下没有一种方法是工作在5.0 JRE禁用按钮,
所以我添加了下面code禁用和
则该按钮的禁用功能是工作,但setchangelistener不工作
这是我的问题
..这是我的code,我添加了禁用button..please检查。我是否需要调用的操作方法去改变什么?

 公共布尔isDisable(){
   返回isDisable;
 } 公共无效setDisable(布尔isDisable){
   this.isDisable = isDisable;
 无效();
 } 公共布尔isFocusable(){
   返回isFocusable&放大器;&安培; !isDisable;
 } 公共无效setFocusable(布尔isFocusable){
 this.isFocusable = isFocusable;
 } 保护布尔invokeAction(INT行动){
   如果(!isDisable){
    fieldChangeNotify(0);
   }       返回true;
 } 公共布尔的setEnabled(){
   返回false;
 }


解决方案

下面是一个<一个href=\"http://supportforums.blackberry.com/t5/Java-Development/how-to-enable-disable-some-fields-based-on-checkbox-value/td-p/552247\"相对=nofollow>在这个黑莓论坛讨论。

我已经做了,有时实际上是利用 isEditable()属性对字段的对象,因为可编辑和被使能有几分相似的概念。如果你真的想保持独立的 isDisabled() code,这很好。刚刚替补,以下,我用 isEditable()(记住扭转布尔...这是一个原因一直计划肯定...让你的方法 isEnabled()而不是 isDisabled())。

所以,不是任何code你上面贴的,我只想补充这code为 BitmapButtonField BaseButtonField

公共布尔isFocusable(){
   返回isEditable()及&放大器; super.isFocusable();
}

而这 BitmapButtonField

保护无效漆(图形G){
   INT oldAlpha = g.getGlobalAlpha();
   INT指数= g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS)? FOCUS:正常;
   如果(!isEditable()){
      g.setGlobalAlpha(100); // alpha是0至255,所以这是二百五十五分之一百
   }
   g.drawBitmap(0,0,_bitmaps [指数] .getWidth(),_bitmaps [指数] .getHeight(),_bitmaps [指数],0,0);
   g.setGlobalAlpha(oldAlpha);
}

然后,我可以设置一个变化监听,或禁用按钮,这样在我管理器类:

BitmapButtonField BTN =
     新BitmapButtonField(Bitmap.getBitma $ P $的PSource(button.png),
                           Bitmap.getBitma $ P $的PSource(按钮lit.png));  btn.setChangeListener(新FieldChangeListener(){
     公共无效fieldChanged(场场,诠释上下文){
        Dialog.alert(按钮点击!);
     }
  });
  btn.setEditable(假); //这将禁用按钮
  加(BTN);

不过,明白,如果你的禁用按钮,这意味着你的改变侦听器将不会被调用。这是一种它是如何工作的。如果按钮的启用,因此可点击更改侦听器时才能调用。

另外,还要注意的是,为了禁用时,使按钮看起来不同(没有的编辑的),我重写的paint()设置当按钮被禁用不同的alpha值。你没有提到这一点,所以如果你不喜欢它,你当然可以把它拿出来。

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

解决方案

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.

这篇关于如何在黑莓高级UI的例子使用bitmapbuttonfield禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆