1

我需要为自定义管理器设置点击/点击事件。

管理器包含几个元素,但我需要整个管理器响应事件,因此如果用户点击/单击管理器内的任何元素,也应该调用事件处理程序。我所做的是将一个changeListener附加到一个采用所有宽度和高度的NullField,但永远不会调用fieldChanged方法

public class TestManager extends Manager{
    private BitmapField _icon;
    private LabelField _lblTitle;
    private NullField nullField;
    public TestManager(Bitmap pIcon){
        super(Manager.FOCUSABLE);
        _icon = new BitmapField(pIcon);
        _lblTitle = new LabelField("This is the title");
        nullField = new NullField(NullField.FOCUSABLE);
        add(_icon);
        add(_lblTitle);
        add(nullField);
            nullField.setChangeListener(new FieldChangeListener() {

        public void fieldChanged(Field field, int context) {
            doSomething();
        }
    });

    }

    public void doSomething(){
    System.out.println("do something!!");
}
    public void paint(Graphics graphics)
    {
       graphics.setColor(Color.DELIMITER_COLOR);
       graphics.fillRect(0, 20, Display.getWidth(), 50);

       super.paint(graphics);
    }

    protected void paintBackground(Graphics arg0) {
       arg0.setColor(Color.LIGHTGRAY);
       arg0.fillRect(0, 0, Display.getWidth(), Display.getHeight());
       super.paint(arg0);
    }

    protected void sublayout(int width, int height) {
        setPositionChild(_icon, UIFactory.getBestX(10), UIFactory.getBestX(8));
        setPositionChild(_lblTitle, UIFactory.getBestX(60), _topMarginTitle!=0?UIFactory.getBestX(_topMarginTitle):UIFactory.getBestX(15));
        setPositionChild(nullField, 0, 0);
        layoutChild(_icon, width, UIFactory.getBestX(50));
        layoutChild(_lblTitle, width, UIFactory.getBestX(50));
        layoutChild(nullField, width, height);
        setExtent(width, height);
    }

      protected boolean navigationClick(int status, int time){ 
            invalidate();
            doSomething();
            return super.navigationClick(status, time);
        }

}

我应该如何让整个经理响应点击/点击?

我正在使用 API 5.0

提前致谢!

4

1 回答 1

1

在 BB 论坛中也有人问过问题,我在那里做出了回应: BlackBerry-How-to-properly-handle-a-TAP-CLICK-in-a-custom

除此之外,这里有一个解决问题的方法:

public class BaseTouchManager extends Manager {

    private FieldChangeListener _callBack;

    public BaseTouchManager(FieldChangeListener pCallBack) {
        super(0);
        _callBack = pCallBack;
        setChangeListener(new FieldChangeListener() {

            public void fieldChanged(Field field, int context) {
                _callBack.fieldChanged(field, context);
            }
        });

        add(new NullField(Field.FOCUSABLE));
    }

    protected void sublayout(int width, int height) {
        setExtent(Display.getHeight()/2, 100);
    }

    protected boolean navigationClick(int status, int time) {
            fieldChangeNotify(1);
            return true;
    }

    public void paint(Graphics graphics) {
        if(_callBack!=null){
            if(isFocus())
                graphics.setColor(Color.LIGHTBLUE);
            else
                graphics.setColor(Color.WHITE);
            graphics.fillRect(this.getWidth() - 10, 
                    this.getHeight() - 10, 
                    10, 10);

        }
        super.paint(graphics);  
    }
}

原始发帖人实际上提出了不同的解决方案,请查看 BB 论坛帖子了解更多信息。

于 2013-10-15T22:12:31.200 回答