如何处理ButtonField字段和BitmapField点击黑莓风暴(触摸)事件?(How to

2019-08-03 02:29发布

我创建了一个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...");
    }
   }
}

但ButtonField字段或BitmapField点击也不会在黑莓4.7模拟器发生。

我想建立它的黑莓风暴所以我为使用黑莓4.7

如何处理点击/触摸查询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;
 }

但每次相同的按钮被调用,其具有焦点。

也就是说,如果“确定”按钮是其重点则即使ü点击“删除”按钮“确定”按钮调用。

因此,如何改变对焦点上按钮点击? 意味着无论是点击ButtonField字段或BitmapField,应该得到重点是什么?

有没有检查“button.isClicked()之类button.isFocus()”的任何方法?

Answer 1:

首先,不要忘了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);
    }
}


文章来源: How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?