2012-03-19 6 views
0

私が焦点にbuttonfieldカスタムのビットマップを変更しようとしています。私は2つの画像を持っています.1つは灰色で、もう1つは赤で、ボタンに焦点が当てられたら交換したいと思います。フォーカスのカスタムボタンフィールドのビットマップを変更するには? (ブラックベリー)

このコードはボタンがグレーから赤に向かう、第二の画分について正しい値にけいれん作っています。焦点を合わせると、それが選択され、不要なイベントが生成されます。変更後、元の色に戻り、シミュレータがフリーズします。

は、誰もが私たちは、このコードでいただきました!間違って見つけることができますか?

マイカスタムbuttonFieldクラス:

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.system.Characters; 
import net.rim.device.api.ui.Color; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.component.BitmapField; 

public class ImageNavbarButtonField extends BitmapField{ 

private Bitmap image; 
private boolean isFocused; 

public ImageNavbarButtonField(Bitmap image) { 
    super(image); 
    this.image = image; 
} 

public boolean isFocusable() { 
    return true; 
} 

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

protected boolean trackwheelClick(int status, int time) { 
    fieldChangeNotify(0); 
    return true; 
} 

protected void drawFocus(Graphics graphics, boolean on){} 
public void onFocus(){ 
     setBitmap(ImageResizer.generateHitBitmap(image)); 
     invalidate(); 
} 

public void onUnfocus(){ 
     //super.onUnfocus(); 
     setBitmap(image); 
     invalidate(); 
} 

protected void paint(Graphics graphics) { 
    super.paint(graphics); 
} 

protected boolean keyChar(char character, int status, int time) { 
    if(Characters.ENTER == character || Characters.SPACE == character) { 
     fieldChangeNotify(0); 
     return true; 
    } 
    return super.keyChar(character, status, time); 
} 

}あなたはこのようなあなたのcustomButtonを作成することができ

答えて

2

この

final imagebutton sav_users = new imagebutton("",Field.FOCUSABLE, "normalimage.png","mouseoverimage.png", 0x9cbe95); 

IMAGEBUTTONのclasssを試みるが与えられているベル

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

public class imagebutton extends Field { 

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 = getFont(); 
    _label = text; 
    _labelHeight = _onPicture.getHeight(); 
    _labelWidth = _onPicture.getWidth(); 

    this.color = color; 

    _currentPicture = _offPicture; 
} 

/** 
* @return The text on the button 
*/ 
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(); 
} 

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

/** 
* Field implementation. 
* @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean) 
*/ 
protected void drawFocus(Graphics graphics, boolean on) { 
    // Do nothing 
} 

/** 
* 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.BLACK); 
    graphics.setFont(_font); 
    graphics.drawText(_label, 4, 2, 
     (int)(getStyle() & DrawStyle.ELLIPSIS | 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; 
} 

} 
1

public class ImageNavbarButtonField extends Field{ 

    protected Bitmap image; 
    protected Bitmap inverseImage; 
    protected int fieldWidth; 
    protected int fieldHeight; 

    public ImageNavbarButtonField (Bitmap image,long style) { 
     super(Field.FOCUSABLE | style); 
     this.image = image; 
     this.inverseImage = ImageResizer.generateHitBitmap(image); 
     fieldHeight = image.getHeight(); 
     fieldWidth = image.getWidth(); 
    } 
    public int getPreferredWidth() { 
     return fieldWidth; 
    } 
    public int getPreferredHeight() { 
     return fieldHeight; 
    } 
    protected void paint(Graphics graphics) { 
     if (isFocus()){ 
      graphics.drawBitmap(0,0,fieldWidth,fieldHeight,inverseImage,0,0); 
     }else{ 
      graphics.drawBitmap(0,0,fieldWidth,fieldHeight,image,0,0); 
     } 
    } 
    protected boolean navigationClick(int status, int time) { 
     fieldChangeNotify(0); 
     return true; 
    } 

    protected boolean trackwheelClick(int status, int time) { 
     fieldChangeNotify(0); 
     return true; 
    } 
    protected boolean keyChar(char character, int status, int time) { 
     if(Characters.ENTER == character || Characters.SPACE == character) { 
      fieldChangeNotify(0); 
      return true; 
     } 
     return super.keyChar(character, status, time); 
    } 
} 
1

使用PictureBackgroundButtonFieldクラスow- Buttonfieldまたはyourcustom classのinsted。以下はクラスです。 `package com.picturebackgroundbuttonfield;

輸入net.rim.device.api.ui。 ; import net.rim.device.api.system。;ボタンの背景として画像を使用する方法を示し

/** * カスタムボタンフィールド。 */ パブリッククラスPictureBackgroundButtonFieldはフィールド {
プライベート文字列_labelを拡張します。 プライベートint _labelHeight; プライベートint _labelWidth; プライベートフォント_font; プライベートビットマップ_onPicture、_offPicture; プライベートビットマップ_currentPicture;

/** 
* Constructor. 
* @param text The text to be displayed on the button 
* @param style Combination of field style bits to specify display attributes 
*/ 
public PictureBackgroundButtonField(Bitmap onFocus, Bitmap offFocus, String text, long style) 
{ 
    super(style); 
    _onPicture = onFocus; 
    _offPicture = offFocus; 
    _font = getFont(); 
    _label = text; 
    _labelHeight = _onPicture.getHeight(); 
    _labelWidth = _onPicture.getWidth(); 
    _currentPicture = _offPicture; 
} 

/** 
* @return The text on the button 
*/ 
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; 
    // setFont(getFont().derive(Font.BOLD)); 
    invalidate(); 
} 

/** 
* Field implementation. Changes picture back when focus is lost. 
* @see net.rim.device.api.ui.Field#onUnfocus() 
*/ 

protected void onUnfocus() 
{ 
    _currentPicture = _offPicture; 
    // setFont(getFont().derive(Font.PLAIN)); 
    invalidate(); 
} 

/** 
* Field implementation. 
* @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean) 
*/ 
protected void drawFocus(Graphics graphics, boolean on) 
{ 
    // Do nothing 
} 

/** 
* 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) 
*/ 

/** 
* 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; 
} 

/*protected void paint(Graphics graphics) { 
    // TODO Auto-generated method stub 

}*/ 

protected void paint(Graphics graphics) 
{  
    graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0); 
    graphics.setBackgroundColor(Color.BLACK); 
    graphics.drawText(_label, 2, 0, 
      (int)(getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK), 
      getWidth() - 6); 
} 

} `

以下の方法でusetそれを..

PictureBackgroundButtonField button = new PictureBackgroundButtonField(onfocusimage,offfocusimage,"",Field.HCENTER); 
manager.add(button); 
関連する問題