5

我創建了一個ButtonField字段&一個BitmapField如..如何處理ButtonField和BitmapField在Blackberry Storm中單擊(觸摸)事件?

public class MyCanvas extends MainScreen implements FieldChangeListener 
    { 
    HorizontalFieldManager hfm; 
    private Bitmap startBitmap; 
    private BitmapField startBitmapField; 
    private ButtonField okButton; 

    MyCanvas() 
    { 
    hfm = new HorizontalFIeldManager(); 
    startBitmap = Bitmap.getBitmapResource("start.png"); 
    startBitmapField = new BitmapField(startBitmap); 
    startBitmapField.setChangeListener(this); 
    hfm.add(startBitmapField); 

    okButton = new ButtonField("Ok", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY); 
    okButton.setChangeListener(this); 
    hfm.add(okButton); 
    } 

    public void fieldChanged(Field field, int context) 
    { 
    if(field == startBitmapField) 
    { 
     System.out.println("Touched START..."); 
    } 
    else if(field == okButton) 
    { 
     System.out.println("Touched Ok..."); 
    } 
    } 
} 

但ButtonField字段或BitmapField點擊也不會在黑莓4.7模擬器發生。

我想建立它的黑莓風暴所以我米使用黑莓4.7

如何處理點擊/觸摸查詢ButtonField字段&事件BitmapField黑莓風暴?


我米作爲

okButtonField = new ButtonField("Ok", BitmapField.HIGHLIGHT_SELECT | BitmapField.FOCUSABLE); 

startBitmapField = new BitmapField(startBitmap, BitmapField.HIGHLIGHT_SELECT | BitmapField.FOCUSABLE); 

及其創建ButtonField字段& BitmapFields有工作..

protected boolean touchEvent(TouchEvent event) 
{ 
    switch(event.getEvent() ) 
    { 
    case TouchEvent.DOWN: ........ 
      return true; 
    case TouchEvent.MOVE: ....... 
          return true; 
    case TouchEvent.UP: ........ 
          return true; 

    case TouchEvent.CLICK: 
     if(deleteButton.isFocus()) 
     {    
     System.out.println("Touched DEL .........."); 
     } 
     else if(okButton.isFocus()) 
     {    
     System.out.println("Touched OK .........."); 
     } 
     else if(startBitmapField.isFocus()) 
     {    
     System.out.println("Touched START .........."); 
     }   
    return true; 
    } 
    return false; 
} 

但每次相同的按鈕被調用,其具有焦點。

意思是如果「確定」按鈕有焦點,即使你點擊了「刪除」按鈕「確定」按鈕被調用。

那麼如何改變按鈕點擊的重點?意味着點擊ButtonField或BitmapField的任何應該獲得焦點?

是否有任何方法檢查「button.isClicked()像button.isFocus()」?

回答

5

首先,不要忘了添加hfm到屏幕;)
實際上按鈕單擊工作正常。
現在,爲了使位圖點擊也可以工作,爲您的BitmapField實現受保護的布爾touchEvent(TouchEvent消息)。創建擴展類將會更好:

class MyCanvas extends MainScreen implements FieldChangeListener { 
    HorizontalFieldManager hfm; 
    private Bitmap startBitmap; 
    private BitmapField startBitmapField; 
    private ButtonField okButton; 
    private ButtonField cancelButton; 

    MyCanvas() { 
     hfm = new HorizontalFieldManager(); 
     add(hfm); 

     startBitmap = Bitmap.getBitmapResource("start.png"); 
     startBitmapField = new TouchBitmapField(startBitmap); 
     startBitmapField.setChangeListener(this); 
     hfm.add(startBitmapField); 

     okButton = new ButtonField("Ok", ButtonField.CONSUME_CLICK 
       | ButtonField.NEVER_DIRTY); 
     okButton.setChangeListener(this); 
     hfm.add(okButton); 

     cancelButton = new ButtonField("Cancel", ButtonField.CONSUME_CLICK 
       | ButtonField.NEVER_DIRTY); 
     cancelButton.setChangeListener(this); 
     hfm.add(cancelButton); 
    } 

    public void fieldChanged(Field field, int context) { 
     if (field == startBitmapField) { 
      System.out.println("Touched START..."); 
     } else if (field == okButton) { 
      System.out.println("Touched Ok..."); 
     } else if (field == cancelButton) { 
      System.out.println("Touched Cancel..."); 
     } 
    } 
} 

class TouchBitmapField extends BitmapField { 
    public TouchBitmapField(Bitmap startBitmap) { 
     super(startBitmap); 
    } 

    protected boolean touchEvent(TouchEvent message) { 
     if (TouchEvent.CLICK == message.getEvent()) { 
      FieldChangeListener listener = getChangeListener(); 
      if (null != listener) 
       listener.fieldChanged(this, 1); 
     } 
     return super.touchEvent(message); 
    } 
} 
+0

是的。我添加hfm到屏幕上。其實有2個按鈕。那麼如何處理它的點擊事件。現在我正在處理button.setChangeListener(this)並重寫它的fieldChanged()方法作爲我上面的代碼。但沒有效果。那麼如何處理2個按鈕的觸摸事件呢?我會嘗試urmap代碼爲bitmapFields。 – Shreyas 2009-08-31 04:55:29

+0

Shreyas,我更新了一個按鈕的代碼,它仍然在工作......也許這取決於別的東西?檢查它,如果它仍然無法正常工作,可以發佈完整的代碼,以便我們弄清楚。 – 2009-08-31 10:39:15

+0

可以觸摸模擬器4.7.0.41上的ButtonFields和BitmapFields嗎? – Shreyas 2009-08-31 11:15:00