首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Blackberry点击事件

Blackberry点击事件
EN

Stack Overflow用户
提问于 2013-05-15 16:26:14
回答 1查看 284关注 0票数 1

我在VerticalFieldManager中有一个ImageButton,在HorizontalFieldManager中有VFM。当我单击周围字段中的任意位置时,将触发ImageButton单击事件。我希望单击事件仅在单击ImageButton时触发,而不是字段(VFM或HFM)。

代码如下:

代码语言:javascript
复制
ImageButton Login = new ImageButton(configModel.getLoginButton(), FOCUSABLE|Field.FIELD_RIGHT, "login.png", "plogin.png",0x9cbe95);
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH);
VerticalFieldManager loginButtonVfm = new VerticalFieldManager(USE_ALL_WIDTH);

loginButtonVfm.add(Login);
hfm.add(loginButtonVfm);
add(hfm);


FieldChangeListener loginListener = new FieldChangeListener() { 
public void fieldChanged(Field field, int context) {
     Dialog.alert("Fired");
    }
}
Login.setChangeListener(loginListener); 

ImageButton类代码如下:

代码语言:javascript
复制
public class ImageButton extends Field{

        //Image Button Class 
        private String _label;
        private int _labelHeight;
        private int _labelWidth;
        private Font _font;

        private Bitmap _currentPicture;
        private Bitmap _onPicture;
        private Bitmap _offPicture;
        int color;

        public ImageButton(String text, long style ,String img, String img_hvr, int color){
            super(style);

            _offPicture = Bitmap.getBitmapResource(img);
            _onPicture = Bitmap.getBitmapResource(img_hvr);

            _font = Font.getDefault().derive(Font.BOLD, 7, Ui.UNITS_pt);
            _label = text;


            _labelHeight = _onPicture.getHeight();  
            _labelWidth = _onPicture.getWidth();

            this.color = color;

            _currentPicture = _offPicture;
        }
        public void setImage(String img){

            _offPicture = Bitmap.getBitmapResource(img); 
            _currentPicture = _offPicture;
        }

        /**
         * @return The text on the button
         */
        public void setText(String text){
            _label = text;
        }
            String getText(){
            return _label;
        }

        /**
         * Field implementation.
         * @see net.rim.device.api.ui.Field#getPreferredHeight()
         */
        public int getPreferredHeight(){
            return _labelHeight;
        }
        /**
         * Field implementation.
         * @see net.rim.device.api.ui.Field#getPreferredWidth()
         */
        public int getPreferredWidth(){
            return _labelWidth;
        }

        /**
         * Field implementation.  Changes the picture when focus is gained.
         * @see net.rim.device.api.ui.Field#onFocus(int)
         */
        protected void onFocus(int direction) {

             _currentPicture = _onPicture;
          //  invalidate();
            super.onFocus(direction);
        }

        /**
         * Field implementation.  Changes picture back when focus is lost.
         * @see net.rim.device.api.ui.Field#onUnfocus()
         */
        protected void onUnfocus() {
            _currentPicture = _offPicture;
            invalidate();
            super.onUnfocus();
        }

        /**
         * Field implementation.  
         * @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
         */
//      protected void drawFocus(Graphics graphics, boolean on) {
//          // Do nothing
//      }
        protected void drawFocus(Graphics graphics, boolean on) {
            if (on) {
                     //draw your own custom focus.
                    }
                }
        /**
         * Field implementation.
         * @see net.rim.device.api.ui.Field#layout(int, int)
         */
        protected void layout(int width, int height) {
            setExtent(Math.min( width, getPreferredWidth()),
            Math.min( height, getPreferredHeight()));
        }
        /**
         * Field implementation.
         * @see net.rim.device.api.ui.Field#paint(Graphics)
         */
        protected void paint(Graphics graphics){      
            // First draw the background colour and picture
            graphics.setColor(this.color);
            graphics.fillRect(0, 0, getWidth(), getHeight());
            graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);

            // Then draw the text
            graphics.setColor(Color.WHITE);
            graphics.setFont(_font);
            graphics.setFont(graphics.getFont().derive(Font.BOLD)); 
            graphics.drawText(_label, 5,9,
                (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.VALIGN_MASK | DrawStyle.HALIGN_MASK),
                getWidth() - 6 );

        }

        /**
         * Overridden so that the Event Dispatch thread can catch this event
         * instead of having it be caught here..
         * @see net.rim.device.api.ui.Field#navigationClick(int, int)
         */
        protected boolean navigationClick(int status, int time){
            fieldChangeNotify(1);
            return true;
        } 
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-15 16:46:45

当您尝试自己编写自定义字段时,这类事情实际上很容易找到bug。)这不是你的错。BlackBerry Java使这件事变得比实际情况下更难!)

我认为解决这个问题最简单的方法是重用别人已经测试过的东西。我建议您查看BlackBerry Advanced UI库,特别是这两个类:

BaseButtonField

BitmapButtonField

因为我认为他们做的就是你需要的。我认为您需要更改或添加到ImageButton类中的重要代码如下:

代码语言:javascript
复制
protected boolean keyChar( char character, int status, int time ) 
{
    if( character == Characters.ENTER ) {
        clickButton();
        return true;
    }
    return super.keyChar( character, status, time );
}

protected boolean navigationClick( int status, int time ) 
{
    if (status != 0) clickButton(); 
    return true;    
}

protected boolean trackwheelClick( int status, int time )
{        
    if (status != 0) clickButton();    
    return true;
}

protected boolean invokeAction( int action ) 
{
    switch( action ) {
        case ACTION_INVOKE: {
            clickButton(); 
            return true;
        }
    }
    return super.invokeAction( action );
}    

protected boolean touchEvent( TouchEvent message )
{
    int x = message.getX( 1 );
    int y = message.getY( 1 );
    if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
        // Outside the field
        return false;
    }
    switch( message.getEvent() ) {

        case TouchEvent.UNCLICK:
            clickButton();
            return true;
    }
    return super.touchEvent( message );
}

/**
 * A public way to click this button
 */
public void clickButton() 
{
    fieldChangeNotify( 0 );
}

然后,您将能够像现在一样使用FieldChangeListener,但图像按钮将不会调用它,除非单击按钮上的

如果您愿意,当然可以将上面的代码更改为使用

代码语言:javascript
复制
    fieldChangeNotify( 1 );

如果1对您来说是一个有意义的上下文值。重要的是添加其他方法,并更改navigationClick()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16560168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档