BlackBerry 客户定制组件(一) PopImageLabelField (一个可以改变状态的按钮)

BlackBerry 客户定制组件(一) PopImageLabelField (一个可以改变状态的按钮)

效果图:


点击后:


这是放出的第一个客户自定制组件。比较简单。
黄色框,是改变了系统的焦点事件的效果。
行为是这样的,点击 PopImageLabelField 将会改变图标
这里是向上的箭头和向下的箭头的交替变化。


 

这可以用来提示,现在按钮的状态。
有两个类:
ImageLabelField 是一个带图标的标志符。它也可以被单独作为一个静止的按钮使用。
PopImageLabelField  继承了 ImageLabelField,并可以接受点击事件,改变并显示Image。


/*
 * ImageLabel.java
 *
 * FlyCloud mobi, 2003-2008
 * guolin.mobi*gmail.com
 * Confidential and proprietary.
 */

package app.ui.component;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

public class ImageLabelField extends Field{
   
    protected Bitmap image;
    protected String text;
    protected int bgColor = 0x00FFFFFF;
    protected int fontColor = 0x00000000;
               
    public ImageLabelField(String text, long style)
    {
        super( style );
        this.text = text;
    }
   
    public ImageLabelField( Bitmap image )
    {
        super( Field.FOCUSABLE );
        this.image = image ;
    }
   
    public ImageLabelField(String imagePath, String text )
    {
        super( Field.FOCUSABLE );
        this.text = text;
        image = Bitmap.getBitmapResource( imagePath );   
    }
   
    public ImageLabelField( Bitmap image, String text)
    {
        super( Field.FOCUSABLE );
        this.text = text;
        this.image = image;
    }
   
    protected void layout(int width, int height)
    {
        setExtent( getPreferredWidth(), getPreferredHeight() );
    }
   
    protected void paint( Graphics g)
    {
        // background
        g.setColor( bgColor );
        g.fillRect( 0, 0 , getPreferredWidth(), getPreferredHeight() );
       
        // image
        int x = 0;
        if( image != null )
        {
            int imageWidth =  image.getWidth();
            int imageHeight = image.getHeight();
           
            g.drawBitmap( 1, ( getPreferredHeight() - imageHeight )/2,
                         image.getWidth(), image.getHeight(), image, 0, 0);
            x = image.getWidth() + 2;
        }
       
        // Text
        g.setColor( fontColor );
        if( text != null )
        g.drawText( text, x, 0);
    }
   
    public int getPreferredWidth()
    {
        int imageWidth = 0 ;
        int textWidth = ( new LabelField( text ) ).getPreferredWidth();
       
        if( image != null )
        imageWidth = image.getWidth();
       
        return imageWidth +  textWidth + 2;
    }
   
    public int getPreferredHeight()
    {
        int imageHeight = 0;
        int textHeight = ( new LabelField( text ) ).getPreferredHeight();
       
        if( image != null )
        imageHeight = image.getHeight();
       
        return (imageHeight > textHeight ? imageHeight : textHeight) + 2;
    }
   
    protected void drawFocus(Graphics g, boolean on)
    {
        int tmpColor = g.getColor();
        g.setColor( app.ui.skin.CustomerColor.FOCUS_RECT );
        g.drawRect( 0, 0, getPreferredWidth(), getPreferredHeight() );
        g.setColor( tmpColor );
    }
    
    protected boolean keyDown(int keycode, int time)
    {
        if( Keypad.key( keycode ) == Keypad.KEY_ENTER)
        { 
                invalidate();
        } 
        else
        {
          fieldChangeNotify(1);
          return false;
        }
       
        fieldChangeNotify(1);
        return true;
    }   
}


/*
 * PopImageLabel.java
 *
 * FlyCloud, 2003-2008
 * guolin.mobi*gmail.com
 * Confidential and proprietary.
 */

package app.ui.component;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

public class PopImageLabelField extends ImageLabelField
{   
    private Bitmap imageUp;
    private Bitmap imageDown;
    private boolean statusUp;
   
    public PopImageLabelField( String text, long style )
    {
        super( text , style );
        imageUp = Bitmap.getBitmapResource( "arrow_collapse.gif" );
        imageDown = Bitmap.getBitmapResource( "arrow_expand.gif" );
        this.statusUp = false;
        image = imageDown;
    }
   
    public PopImageLabelField( String pathImageUp, String pathImageDown, boolean statusUp, String text, long style)
    {
        super( text , style);
       
        imageUp =  Bitmap.getBitmapResource( pathImageUp );
        imageDown = Bitmap.getBitmapResource( pathImageDown );
       
        this.statusUp = statusUp;
        if( this.statusUp )
            image = imageUp;
        else
            image = imageDown;
    }
   
    public synchronized void keyAction()
    {
        statusUp = !statusUp;
       
        if( this.statusUp )
            image = imageUp;
        else
            image = imageDown;

        invalidate();
    }
   
    protected boolean keyDown(int keycode, int time)
    {
        if( Keypad.key( keycode ) == Keypad.KEY_ENTER)
        {
           keyAction();
           invalidate();
        } 
        else
        {
          fieldChangeNotify(1);
          return false;
        }
       
        fieldChangeNotify(1);
        return true;
    }
   
    protected void fieldChangeNotify(int context)
    {
        try {
            this.getChangeListener().fieldChanged(this, context);
        } catch (Exception exception) {
        }
    } 
}

你可能感兴趣的:(BlackBerry 客户定制组件(一) PopImageLabelField (一个可以改变状态的按钮))