2012-07-16 50 views
1

我想改變背景按鈕的顏色和onfocus上的文字顏色 我該如何製作它?自定義按鈕字段黑莓:onfocus,onUnfocus

class RoundedRectField extends Field { 

    // Layout values 
    private static final int CURVE_X = 12; // X-axis inset of curve 
    private static final int CURVE_Y = 12; // Y-axis inset of curve 
    private static final int MARGIN = 2; // Space within component boundary 

    // Static colors 
    private static final int TEXT_COLOR = 0xFFFFFF; // White 
    private static final int BORDER_COLOR = 0xFF8000; // dark gray 
    private static final int BACKGROUND_COLOR = 0xFFFFFF; // White 
    private static final int TEXT_COLOR_selected = 0xFF6DB6; 
    private static final int BORDER_COLOR_selected = 0xFF8000; 
    private static final int BACKGROUND_COLOR_selected = 0xCCCCCC; 
    boolean _focus = false; 
    private static String text_button; 
    // Point types array for rounded rectangle. Each point type 
    // corresponds to one of the colors in the colors array. The 
    // space marks the division between points on the top half of 
    // the rectangle and those on the bottom. 
    private static final byte[] PATH_POINT_TYPES = { 
      Graphics.CURVEDPATH_END_POINT, 
      Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT, 
      Graphics.CURVEDPATH_END_POINT, Graphics.CURVEDPATH_END_POINT, 
      Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT, 
      Graphics.CURVEDPATH_END_POINT, 

      Graphics.CURVEDPATH_END_POINT, 
      Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT, 
      Graphics.CURVEDPATH_END_POINT, Graphics.CURVEDPATH_END_POINT, 
      Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT, 
      Graphics.CURVEDPATH_END_POINT, }; 

    // Colors array for rounded rectangle gradient. Each color corresponds 
    // to one of the points in the point types array. Top light, bottom black. 
    private static final int[] PATH_GRADIENT = { 0xFF8000, 0xFF8000, 0xFF8000, 
      0xFF8000, 0xFF8000, 0xFF8000, 

      0xFC0500, 0xFC0500, 0xFC0500, 0xFC0500, 0xFC0500, 0xFC0500 }; 

    // Center our readonly field in the space we're given. 
    public RoundedRectField(String text_button) { 
     super(FIELD_HCENTER | FIELD_VCENTER | READONLY); 
     this.text_button = text_button; 
    } 

    // This field in this demo has a fixed height. 
    public int getPreferredHeight() { 
     return 70; 
    } 

    // This field in this demo has a fixed width. 
    public int getPreferredWidth() { 
     return 240; 
    } 

    // When layout is requested, return our height and width. 
    protected void layout(int width, int height) { 
     setExtent(getPreferredWidth(), getPreferredHeight()); 
    } 

    // When painting is requested, do it ourselves. 
    protected void paint(Graphics g) { 
     // Clear this area to white background, fully opaque. 

      g.clear(); 
      g.setGlobalAlpha(255); 
      g.setBackgroundColor(BACKGROUND_COLOR); 

      // Drawing within our margin. 
      int width = getPreferredWidth() - (MARGIN * 2); 
      int height = getPreferredHeight() - (MARGIN * 2); 

      // Compute paths for the rounded rectangle. The 1st point (0) is on 
      // the left 
      // side, right where the curve in the top left corner starts. So the 
      // top left 
      // corner is point 1. These points correspond to our static arrays. 
      int[] xPts = { 0, 0, CURVE_X, width - CURVE_X, width, width, width, 
        width, width - CURVE_X, CURVE_X, 0, 0 }; 
      int[] yPts = { CURVE_Y, 0, 0, 0, 0, CURVE_Y, height - CURVE_Y, 
        height, height, height, height, height - CURVE_Y }; 

      // Draw the gradient fill. 
      g.drawShadedFilledPath(xPts, yPts, PATH_POINT_TYPES, PATH_GRADIENT, 
        null); 

      // Draw a rounded rectangle for the outline. 
      // I think that drawRoundRect looks better than drawPathOutline. 
      g.setColor(BORDER_COLOR); 
      g.drawRoundRect(0, 0, width, height, CURVE_X * 2, CURVE_Y * 2); 

      // Place some text in the center. 

      Font font = Font.getDefault().derive(Font.PLAIN, 9, Ui.UNITS_pt); 
      int textWidth = font.getAdvance(text_button); 
      int textHeight = font.getHeight(); 
      g.setColor(TEXT_COLOR); 
      g.setFont(font); 
      g.drawText(text_button, (width/2) - (textWidth/2) - MARGIN, 
        (height/2) - (textHeight/2) - MARGIN); 

    } 

    protected void onFocus(int direction) { 
     _focus = true; 
     Dialog.alert("dcd"); 
     invalidate(); 
     super.onFocus(direction); 
    } 

    protected void onUnfocus() { 

     _focus = false; 

     invalidate(); 

     super.onUnfocus(); 
    } 

} 

回答

2

你可以做到這一點的幾種方法。一種常用的方法是在paint()方法中提供自定義焦點圖,該方法已被覆蓋。

你應該能夠做到這一點(我假設你聲明的_selected顏色聚焦狀態):

if (isFocus()) { 
    g.setBackgroundColor(BACKGROUND_COLOR_selected); 
else { 
    g.setBackgroundColor(BACKGROUND_COLOR); 
} 

...

if (isFocus()) { 
    g.setColor(TEXT_COLOR_selected); 
} else { 
    g.setColor(TEXT_COLOR); 
} 

這些線路中去paint(),您正在撥打g.setBackgroundColorg.setColor(TEXT_COLOR)

然後,你會覆蓋drawFocus(),什麼也不做,因爲你的注意力拉在paint()處理:

protected void drawFocus(Graphics graphics, boolean on) { 
    // override superclass implementation and do nothing 
} 

最後,你需要讓你的Field可聚焦,以不斷接收焦點。你可以這樣做是這樣的:

public RoundedRectField(String text_button) { 
    super(FIELD_HCENTER | FIELD_VCENTER | FOCUSABLE); 
    this.text_button = text_button; 
} 

如果您需要的字段設置爲動態可聚焦(有時可成爲焦點,或有時不可作爲焦點),那麼你可以實現此方法:

public boolean isFocusable() { 

但是,如果該字段始終是可以調焦的,然後在構造函數中使用FOCUSABLE標誌將起作用。我測試了這一點,並且我看到文本顏色隨焦點而變化(在OS 5.0 9550上)。

+0

我在按鈕的背景上有顏色。它是一個漸變顏色PATH_GRADIENT = {0xFF8000,0xFF8000,0xFF8000, 0xFF8000,0xFF8000,0xFF8000, 0xFC0500,0xFC0500,0xFC0500,0xFC0500,0xFC0500,0xFC0500}。並在那裏添加isfocus的條件或不是 – mobileDeveloper 2012-07-16 09:30:40

+0

@fou,好吧,沒問題。然後根據isFocus()是true還是false,對'g.drawShadedFilledPath()'進行兩次不同的調用,或者爲focus = true或false聲明兩個不同的PATH_GRADIENT數組值。 – Nate 2012-07-16 09:33:11

+0

我的問題,當我在添加繪畫方法時進行測試if(isFocus())g.setColor(TEXT_COLOR_selected);如果(isFocus()){0128} \t \t \t} else { \t \t \t g.setColor(TEXT_COLOR); \t \t \t}什麼都沒發生 – mobileDeveloper 2012-07-16 09:44:20