2012-07-26 46 views
0

單擊buttonfield時會出現藍色,因爲我setChangeListener()。如果我覆蓋navigationClick(),那麼工作正常。但是,它在TouchEvent上遇到了問題。單擊或使用跟蹤輪單擊按鈕字段時會顯示藍色

這是按鈕調用。

newsbtn = new Custom_ButtonField(news, newsactive, newsactive); 
     newsbtn.setChangeListener(new FieldChangeListener() { 
      public void fieldChanged(Field field, int context) { 
       Main.getUiApplication().pushScreen(
         new Menu_PopupMenu(position)); 
      } 
     }); 
     add(newsbtn); 

這是按鈕聲明。

public class Custom_ButtonField extends ButtonField { 
Bitmap mNormal; 
Bitmap mFocused; 
Bitmap mActive; 

int mWidth; 
int mHeight; 

private int color = -1; 
String text; 

public Custom_ButtonField(Bitmap normal, Bitmap focused, Bitmap active) { 
    super(CONSUME_CLICK | Field.FOCUSABLE | Field.FIELD_HCENTER 
      | Field.FIELD_VCENTER); 
    mNormal = normal; 
    mFocused = focused; 
    mActive = active; 
    mWidth = mNormal.getWidth(); 
    mHeight = mNormal.getHeight(); 
    setMargin(0, 0, 0, 0); 
    setPadding(0, 0, 0, 0); 
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    setBorder(VISUAL_STATE_ACTIVE, 
      BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
} 

public Custom_ButtonField(String text, Bitmap normal, Bitmap focused, 
     Bitmap active, int color) { 
    super(CONSUME_CLICK | Field.FOCUSABLE | Field.FIELD_HCENTER 
      | Field.FIELD_VCENTER); 
    this.color = color; 
    mNormal = normal; 
    mFocused = focused; 
    mActive = active; 
    mWidth = mNormal.getWidth(); 
    mHeight = mNormal.getHeight(); 
    setMargin(0, 0, 0, 0); 
    setPadding(0, 0, 0, 0); 
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    setBorder(VISUAL_STATE_ACTIVE, 
      BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    this.text = text; 
} 

public Custom_ButtonField(String text, Bitmap normal, Bitmap focused, 
     Bitmap active, int color, long style) { 
    super(style); 
    this.color = color; 
    mNormal = normal; 
    mFocused = focused; 
    mActive = active; 
    mWidth = mNormal.getWidth(); 
    mHeight = mNormal.getHeight(); 
    setMargin(0, 0, 0, 0); 
    setPadding(0, 0, 0, 0); 
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    setBorder(VISUAL_STATE_ACTIVE, 
      BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    this.text = text; 
} 

public void setText(String text) { 
    this.text = text; 
    invalidate(); 
} 

public String getText() { 
    return text; 
} 

public void setColor(int color) { 
    this.color = color; 
} 

protected void onFocus(int direction) { 
    super.onFocus(direction); 
    color = 0x540604; 
    this.invalidate(); 
} 

protected void onUnfocus() { 
    super.onUnfocus(); 
    color = Color.WHITE; 
    this.invalidate(); 
} 

protected void paint(Graphics graphics) { 
    int fontcontent; 
    if (Display.getWidth() > 480) 
     fontcontent = 28; 
    else if (Display.getWidth() < 481 && Display.getWidth() > 320) 
     fontcontent = 23; 
    else 
     fontcontent = 18; 

    Bitmap bitmap = null; 
    switch (getVisualState()) { 
    case VISUAL_STATE_NORMAL: 
     bitmap = mNormal; 
     break; 
    case VISUAL_STATE_FOCUS: 
     bitmap = mFocused; 
     break; 
    case VISUAL_STATE_ACTIVE: 
     bitmap = mActive; 
     break; 
    default: 
     bitmap = mNormal; 
    } 
    setBackground(BackgroundFactory.createBitmapBackground(bitmap)); 
    graphics.setFont(Font.getDefault().derive(Font.PLAIN, fontcontent)); 
    graphics.setColor(color); 
    graphics.drawText(text, (mNormal.getWidth() - Font.getDefault() 
      .getAdvance(text))/2, ((mNormal.getHeight() - Font 
      .getDefault().getHeight())/2) + 10, DrawStyle.HCENTER 
      | DrawStyle.VCENTER); 
} 

public int getPreferredWidth() { 
    return mWidth; 
} 

public int getPreferredHeight() { 
    return mHeight; 
} 

protected void layout(int width, int height) { 
    setExtent(mWidth, mHeight); 
} 
} 

即使我重寫drawFocus(){//nothing here},但仍然出現了藍色。

Here是按鈕對焦時的圖像。

Here是單擊時的圖像。

+0

創建兩個圖像。一個用於正常,一個用於鼠標 – Signare 2012-07-26 08:33:20

+1

您不需要在paint方法上設置背景。你可以在構造函數中完成它。使用這種方法,http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/ui/Field.html#setBackground%28int,%20net.rim.device.api .ui.decor.Background%29。您可以在該方法上傳遞所需的視覺狀態和位圖。 – Rupak 2012-07-26 09:11:34

+0

以前是'drawbitmap()'但出現問題,灰色出現 – 2012-07-27 07:32:12

回答

0

這是構造函數之一。

public Custom_ButtonField(String text, Bitmap normal, Bitmap focused, 
     Bitmap active, int color, long style) { 
    super(style); 
    this.color = color; 
    this.text = text; 
    mNormal = normal; 
    mFocused = focused; 
    mActive = active; 
    mWidth = mNormal.getWidth(); 
    mHeight = mNormal.getHeight(); 
    setMargin(0, 0, 0, 0); 
    setPadding(0, 0, 0, 0); 
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    setBorder(VISUAL_STATE_ACTIVE, 
      BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    setBackground(VISUAL_STATE_NORMAL, 
      BackgroundFactory.createBitmapBackground(normal)); 
    setBackground(VISUAL_STATE_FOCUS, 
      BackgroundFactory.createBitmapBackground(focused)); 
    setBackground(VISUAL_STATE_ACTIVE, 
      BackgroundFactory.createBitmapBackground(active)); 
}