0

我想用图像制作一个黑莓按钮,一个图像应该在聚焦时显示,另一个在未聚焦时显示。

甚至你可以在焦点关闭时说蓝色(默认情况下),在焦点打开时说红色,我怎么能做到这一点?

查看我的自定义课程

    import net.rim.device.api.system.Bitmap;
    import net.rim.device.api.ui.Color;
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.Font;
    import net.rim.device.api.ui.FontFamily;
    import net.rim.device.api.ui.Graphics;

     public class MyButtonField extends Field {

     // private int backgroundColour = 0xFFFFFF;
     private Bitmap button;
     private Bitmap on; //= Bitmap.getBitmapResource("img/pink_scribble.bmp");
     private Bitmap off; // = Bitmap.getBitmapResource("img/blue_scribble.bmp");
     private int fieldWidth;
     private int fieldHeight;
    // private int buffer = (Display.getHeight() - 105) / 2;
     private String text;
     private Font fieldFont;
     private boolean btn_text = true;
     private static long style = Field.FIELD_HCENTER | Field.FIELD_VCENTER;

     public MyButtonField(String _text, String onpath, String offpath)  {
      super(Field.FOCUSABLE | style);
      text = _text;
      on = Bitmap.getBitmapResource(onpath);
      off = Bitmap.getBitmapResource(offpath);
      fieldWidth = on.getWidth();
      fieldHeight = on.getHeight();
      button = off;
      fieldFont = FieldFont();
      } 



     public MyButtonField(String _text, String onpath, String offpath, String focusedpath){
             this(_text, onpath, offpath);
     }

     public MyButtonField(String _text, String onpath, String offpath, boolean btn_text) {
     this(_text, onpath , offpath);
     this.btn_text = btn_text;
     }

     public MyButtonField(String _text, String onpath, String offpath, boolean btn_text,     int style, int size)
     {
        this(_text, onpath , offpath, btn_text);
        fieldFont = FieldFont(style, size);
     }
     public MyButtonField(String _text, String onpath, String offpath, long style)
     {
     this(_text, onpath , offpath);
   }
    // public MyButtonField(String _text, String onpath, String offpath, int background)
   // {
   //    this(_text, onpath , offpath);
    ////     backgroundColour = background;
    // }
     protected boolean navigationClick(int status, int time) {
    fieldChangeNotify(1);
    return true;
     }

    protected void onFocus(int direction) {
    button = on;
    invalidate();
   }

     protected void onUnfocus() {
    button = off;
    invalidate();
    }

    public int getPreferredWidth() {
    return fieldWidth;
    }

    public int getPreferredHeight() {
    return fieldHeight;
    }

    protected void layout(int arg0, int arg1) {
    setExtent(getPreferredWidth(), getPreferredHeight());
    } 

    public static Font FieldFont() 
    {
    try {
    FontFamily theFam = FontFamily.forName("SYSTEM");
    return theFam.getFont(net.rim.device.api.ui.Font.PLAIN, 14);
    } catch (ClassNotFoundException ex) {
    ex.printStackTrace();
    }
    return null;
    }

    public static Font FieldFont(int style, int size) 
    {
    try {
    FontFamily theFam = FontFamily.forName("SYSTEM");
    return theFam.getFont(style, size);
    } catch (ClassNotFoundException ex) {
    ex.printStackTrace();
    }
    return null;
    }

    protected void drawFocus(Graphics graphics, boolean on) {
        if (on) {
            //graphics.setColor(Color.RED);

           int width = getWidth();
           int height = getHeight();
           graphics.drawBitmap(0, 0, fieldWidth, fieldHeight, button, 0, 0);

           //Draw a border around the field.
          /*
           graphics.fillRoundRect(0, 0, 3, height,10,10); //Left border
           graphics.fillRoundRect(0, 0, width, 3,10,10); //Top border
           graphics.fillRoundRect(width-3, 0, 3, height,10,10); //Right border
           graphics.fillRoundRect(0, height-3, width, 3,10,10); //Bottom border
         */
           graphics.setColor(Color.GRAY);
        }
        invalidate();
    }

     public void setFocusImage()
     {
     button = on;
     }
     public void setUnFocusImage()
    {
     button = off;
     }

     protected void fieldChangeNotify(int context) {
     this.getChangeListener().fieldChanged(this, context);
     }

    protected void paint(Graphics graphics) 
      {
     graphics.drawBitmap(0, 0, fieldWidth, fieldHeight, button, 0, 0);
     graphics.setFont(getFont().derive(Font.PLAIN, 8));
     graphics.setColor(Color.LIGHTGRAY);
    // graphics.fillRoundRect(0, 0, fieldWidth, fieldHeight, 8, 8);
    //  graphics.setColor(Color.GRAY);
        //graphics.drawRoundRect(0, 0, fieldWidth, fieldHeight, 8, 8);

     if(btn_text)
         graphics.drawText(text, 10, 5);
    }

       public String getLabel() {
        return text;
        }
        }
4

3 回答 3

1

您应该覆盖onFocus(),onUnfocus()paint().

请参阅自定义按钮字段文章。

于 2011-07-21T14:23:43.517 回答
0

你可能错过了这一点:

public boolean isFocusable() {
    return true;
}

这应该在您的自定义Field中,以便 BB UI 框架理解它是一个可聚焦的字段。是的,这很奇怪,因为您已经Field.FOCUSABLE在构造函数中传递了,但是请尝试一下。

于 2011-07-22T06:47:08.517 回答
0

为什么要覆盖 drawFocus() 方法。由于您要覆盖绘制方法和 onFocus 和 onUnFocus 来更改位图,因此您无需在 drawFocus() 中执行任何操作。使用paint方法绘制任何东西。每次调用 Invalide() 时,它都会要求屏幕/字段重新绘制自己。因此,从 drawFocus 中删除所有代码并将其添加到 paint 方法中。

protected void drawFocus(Graphics graphics, boolean on) 
{
    // Do nothing
}

protected void paint(Graphics graphics) 
{
        graphics.drawBitmap(0, 0, fieldWidth, fieldHeight, button, 0, 0);
        graphics.setFont(getFont().derive(Font.PLAIN, 8));
        graphics.setColor(Color.LIGHTGRAY);

        if(btn_text)
            graphics.drawText(text, 10, 5);
}

希望它能解决你的问题。

于 2011-07-22T19:53:47.947 回答