如何处理ButtonField& BitmapField在Blackberry Storm中单击(触摸)事件?

时间:2009-08-29 10:37:18

标签: user-interface blackberry blackberry-storm touchscreen rim-4.7

我创建了一个ButtonField&一个BitmapField,如..

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

但在Blackberry 4.7模拟器中没有发生ButtonField或BitmapField点击。

我想为Blackberry Storm构建它,所以我使用的是Blackberry 4.7

如何处理ButtonField&的点击/触摸事件Blackberry Storm的BitmapField?


我正在创建ButtonField& BitmapFields为

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

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

与...合作

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

但每次调用具有焦点的相同按钮时。

表示如果“确认”按钮有焦点,那么即使你点击“删除”按钮,也会调用“确认”按钮。

那么如何改变对Button Click的关注?是指单击ButtonField或BitmapField,是否应该获得焦点?

是否有任何方法可以检查“button.isClicked(),例如button.isFocus()”?

1 个答案:

答案 0 :(得分:5)

首先,不要忘记将hfm添加到屏幕;)
实际上按钮点击工作正常 现在,为了使位图单击工作,也为BitmapField实现受保护的boolean 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);
    }
}