5

我创建了一个 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

如何处理 Blackberry Storm 的 ButtonField 和 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() like button.isFocus()”吗?

4

1 回答 1

5

首先,不要忘记将 hfm 添加到屏幕;)
实际上按钮单击工作正常。
现在,要使位图点击也能正常工作,请为您的 BitmapField 实现受保护的布尔 touchEvent(TouchEvent message)。创建扩展类会更好:

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);
    }
}
于 2009-08-30T07:22:26.700 回答