2011-07-21 28 views
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; 
     } 
     } 
+0

Thanx爲編輯,但傷心,仍然沒有答案,即使這個問題並不難(因爲我是一個iPhone開發人員),我不知道是什麼在BB –

回答

1

您應該重寫onFocus()onUnfocus()paint()

請參閱Custom Button Field文章。

+0

非常感謝Onuray沙欣,但我是無法設置焦點,即使我做了我自己的ButtonField,但仍然有問題,我不知道爲什麼;-( –

+0

我更新我的答案,我做了什麼,但親愛的仍然無法理解這一點... –

0

你可能缺少了這一點:

public boolean isFocusable() { 
    return true; 
} 

這應該是你的自定義Field,以便在BB UI框架,瞭解它是一個可聚焦場。是的,這很奇怪,因爲你已經在構造函數中傳遞了Field.FOCUSABLE,不過試試吧。

0

你爲什麼重寫drawFocus()方法。由於您正在重寫paint方法和onFocus onchange以更改位圖,因此不需要在drawFocus()中執行任何操作。使用繪畫方法繪製任何東西。每當你呼叫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); 
} 

希望它能解決您的問題。

相關問題