如何在黑莓中关注它时突出显示按钮?

时间:2011-07-21 11:26:51

标签: blackberry java-me buttonfield

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

甚至你可以在焦点关闭时(默认情况下)说蓝色,在焦点打开时可以说是红色,我怎样才能实现这样的效果?

查看我的自定义类

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

3 个答案:

答案 0 :(得分:1)

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

请参阅Custom Button Field文章。

答案 1 :(得分:0)

你可能错过了这一点:

public boolean isFocusable() {
    return true;
}

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

答案 2 :(得分:0)

为什么要覆盖drawFocus()方法。因为你要重写paint方法和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);
}

希望它能解决你的问题。