2011-06-29 37 views
0

假設我有一個custombuttonField其中包含2張圖圖片,說2個箭頭(一個在左,另一個在右),當此按鈕,用戶點擊箭頭的顏色應該成爲我的應用程序紅色。我通過點擊evevt時按相同的屏幕來做到這一點。我米使用int變量pic_status確定哪些圖象應在custombutonfield被加載上調用pushScreen()。有沒有辦法在不調用pushScreen()的情況下從同一屏幕更新customButtonField(更新位圖)。關於CustomButtonField黑莓

public void fieldChanged(Field field, int context) 
{ 
    if(field == bf1) 
    { 

     if(pic_status == 0) 
     { 
      b =1; 


     } 
     UiApplication.getUiApplication().pushScreen(new Screen2(b));  


    } 

在我的上述標記U已經看到,I M推相同的屏幕,如果用戶點擊 的按鈕。 Plz給出代碼來更新它,而不調用pushScreen()。

回答

1

下面的代碼爲customButtonField具有兩個圖像。一個用於焦點圖像,另一個用於正常圖像。

更新按鈕圖像,你只需要調用setBitmap方法正常圖像。 您可以根據您的要求修改以下代碼。調用setBitmap方法後,您需要調用invalidate()方法。

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.component.BitmapField; 
import net.rim.device.api.ui.component.LabelField; 

class BitmapButtonField extends BitmapField 
{ 
    Bitmap mNormal; 
    Bitmap mFocused; 
    String text; 
    int mWidth; 
    int mHeight; 
    public Bitmap bitmap = null; 

    public BitmapButtonField(String text,Bitmap normal, Bitmap focused) 
    { 
     super(normal,FOCUSABLE); 
     mNormal = normal; 
     mFocused = focused; 
     mWidth = mNormal.getWidth(); 
     mHeight = mNormal.getHeight(); 
     this.text=text; 
     setMargin(0, 0, 0, 0); 
     setPadding(0, 0, 0, 0); 
    } 
    public void setBitmap(Bitmap bitmap) 
    { 
     mNormal=bitmap; 

     this.bitmap=bitmap; 
    } 
    public void setfocusBitmap(Bitmap bitmap) 
    { 
     mFocused=bitmap; 
    } 
    public String getText() 
    { 
     return text; 
    } 
    public void setText(String text) 
    { 
     this.text=text; 
    } 
    protected void paint(Graphics graphics) { 
     Bitmap bitmap = mNormal; 
     if(isFocus()) 
     { 
      bitmap = mFocused; 
     } 
     else 
     { 
      bitmap = mNormal; 
     } 

     graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(), 
         bitmap, 0, 0); 
     LabelField l=new LabelField(text); 

     graphics.drawText(text, bitmap.getWidth()/2-l.getPreferredWidth()/2+3, bitmap.getHeight()/2-l.getPreferredHeight()/2);  
    } 
    protected void drawFocus(Graphics graphics, boolean on) { 
    } 
    protected void onFocus(int direction) { 
     invalidate(); 
     super.onFocus(direction); 
    } 
    protected void onUnfocus() { 
     invalidate(); 
     super.onUnfocus(); 
    } 
    public int getPreferredWidth() { 
     return mWidth; 
    } 

    public int getPreferredHeight() { 
     return mHeight; 
    } 

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

不....上面的代碼是關注焦點並且不重點關注customButtonField。圖片應該改變的時候會有單擊event.Plz幫助... –

+0

您可以使用相同的影像對焦和無焦點的方式。您需要在按鈕單擊時調用setBitmap(new_image)和setfocusBitmap(new_image)。試試吧,這會很好地工作。 –

+0

確定thanx您親切的幫助......其實我沒有注意到的setBitmap()函數,現在它很好地運行,可執行... –