2012-07-23 78 views
0

我重寫了這樣的方法。如何檢測字段是否被觸摸或點擊?

 newsbtn = new Custom_ButtonField(news, newsactive, newsactive) { 
      protected boolean navigationClick(int status, int time) { 
       Main.getUiApplication().pushScreen(
         new Menu_PopupMenu(position)); 
       return true; 
      } 

      protected boolean touchEvent(TouchEvent message) { 
       int eventCode = message.getEvent(); 
       if (eventCode == TouchEvent.UNCLICK){ 
        Main.getUiApplication().pushScreen(
          new Menu_PopupMenu(position)); 
       } 
       return true; 
      } 
     }; 

     add(newsbtn); 

這裏是Custom_ButtonField

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

然而,按鈕無法執行推送畫面,只有setfocus()上的按鈕。

+0

您可以發佈您充分Custom_ButtonField類?我知道你還有另一個問題,但我不確定你是否從那時起進行過修改。謝謝。 – Nate 2012-07-24 08:04:26

回答

2

您不必使用CONSUME_CLICK構造場,只是爲了得到點擊。這決定了字段是否消耗點擊事件,或者讓它們傳播到其他類來處理。但是,海報的代碼已經在他的兩個點擊處理方法,這也意味着返回true「我已經處理了這個點擊...不打擾它傳遞給其他Field班」See more on this here

而且Alan在他的評論中說,他已經在使用CONSUME_CLICK,所以這絕對不是問題。

如果Custom_ButtonField類是相同的you posted here,那麼當我使用您的代碼時,我可以很容易地獲得點擊。但是,我可以看到一個潛在的問題。你不顯示你的Java導入。你的TouchEvent導入看起來像這樣嗎?

import net.rim.device.api.ui.TouchEvent; 

這事實上是在BlackBerry框架另一TouchEvent類,如果你用錯了一個,那麼你已經創建了一個實際上並不覆蓋基類touchEvent()的方法。使用Eclipse快捷方式可以輕鬆導入您的導入,但可能會導致導入錯誤。

我認爲,如果你這樣做,不過,Eclipse應該會顯示一個警告的touchEvent()版本不正確,永遠不會被調用。順便說一下,我通常會在TouchEvent.UNCLICK事件上觸發我的點擊處理代碼,而不是在TouchEvent.CLICK上觸發我的點擊處理代碼。我認爲這是一個更好的用戶界面。點擊不會註冊,直到用戶的手指被擡起。但是,這是一件小事,並不是造成這個問題的原因。

+0

我做了正確導入 – 2012-07-24 02:10:11

+0

當我按一下按鈕,它不僅改變了形象和'focus'僅但不執行'pushscreen' – 2012-07-24 02:20:24

+0

我只是用'TouchEvent.UNCLICK',它可以很好地用於第一次這是推動'Main.getUiApplication()pushScreen( 新Menu_PopupMenu(位置));'但是當我點擊另一個按鈕,它應該工作本身的功能,但它仍然走'Main.getUiApplication()pushScreen( 新Menu_PopupMenu。 (position));' – 2012-07-24 02:47:27

0

你是什麼Custom_ButtonField的家長嗎?

如果它是一個ButtonField字段,你必須設置屬性CONSUME_CLICK在構造

+0

集已,仍不能 – 2012-07-23 12:40:13

相關問題