关于blackberry中的CustomButtonField

关于blackberry中的CustomButtonField,blackberry,Blackberry,假设我有一个custombuttonField,它包含两个位图图片,比如说两个箭头(一个在左边,另一个在右边),当用户点击这个按钮时,箭头的颜色在我的应用程序中应该变成红色。我通过在click evevt上按相同的屏幕来实现这一点。我正在使用一个int变量pic\u status,该变量决定调用推屏()时应该在custombutonfield上加载哪个图片。是否有任何方法可以从同一屏幕更新customButtonField(更新位图),而无需调用pushScreen() 在我上面的代码中,你已经

假设我有一个custombuttonField,它包含两个位图图片,比如说两个箭头(一个在左边,另一个在右边),当用户点击这个按钮时,箭头的颜色在我的应用程序中应该变成红色。我通过在click evevt上按相同的屏幕来实现这一点。我正在使用一个int变量pic\u status,该变量决定调用推屏()时应该在custombutonfield上加载哪个图片。是否有任何方法可以从同一屏幕更新customButtonField(更新位图),而无需调用pushScreen()

在我上面的代码中,你已经看到,如果用户点击,我会推同一个屏幕
按钮。请给出代码,以便在不调用pushScreen()的情况下对其进行更新。

下面是具有两个图像的CustomButton字段的代码。一个用于聚焦图像,另一个用于正常图像

要更新按钮图像,只需为普通图像调用setBitmap方法。 您可以根据需要修改以下代码。在调用setBitmap方法之后,需要调用invalidate()方法

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

class BitmapButtonField extends BitmapField 
{
    Bitmap mNormal;
    Bitmap mFocused;
    String text;
    int mWidth;
    int mHeight;
    public Bitmap bitmap = null;

    public BitmapButtonField(String text,Bitmap normal, Bitmap focused)
    {
        super(normal,FOCUSABLE);
        mNormal = normal;
        mFocused = focused;
        mWidth = mNormal.getWidth();
        mHeight = mNormal.getHeight();
        this.text=text;
        setMargin(0, 0, 0, 0);
        setPadding(0, 0, 0, 0);
    }
    public void setBitmap(Bitmap bitmap)
    {
        mNormal=bitmap;

        this.bitmap=bitmap;
    }
    public void setfocusBitmap(Bitmap bitmap)
    {
        mFocused=bitmap;
    }
    public String getText()
    {
        return text;
    }
    public void setText(String text)
    {
        this.text=text;
    }
    protected void paint(Graphics graphics) {
        Bitmap bitmap = mNormal;
        if(isFocus())
        {
             bitmap = mFocused;
        }
        else
        {
            bitmap = mNormal;
        }

        graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
                        bitmap, 0, 0);
        LabelField l=new LabelField(text);

        graphics.drawText(text,  bitmap.getWidth()/2-l.getPreferredWidth()/2+3,  bitmap.getHeight()/2-l.getPreferredHeight()/2);    
    }
    protected void drawFocus(Graphics graphics, boolean on) {
    }
    protected void onFocus(int direction) {
        invalidate();
        super.onFocus(direction);
    }
    protected void onUnfocus() {
        invalidate();
        super.onUnfocus();
    }
    public int getPreferredWidth() {
        return mWidth;
    }

    public int getPreferredHeight() {
        return mHeight;
    }

    protected void layout(int width, int height) {
        setExtent(mWidth, mHeight);
    }
}

否…上述代码用于在CustomButton字段上聚焦和取消聚焦。图片应该在点击事件发生时更改。Plz帮助…您可以对焦点和取消焦点使用相同的图像。您需要在单击按钮时调用setBitmap(新建图像)和setfocusBitmap(新建图像)。请尝试一下,这将非常有效。好的,谢谢你的热情帮助…实际上我没有注意到setBitmap()函数,现在它运行良好,可执行。。。
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;

class BitmapButtonField extends BitmapField 
{
    Bitmap mNormal;
    Bitmap mFocused;
    String text;
    int mWidth;
    int mHeight;
    public Bitmap bitmap = null;

    public BitmapButtonField(String text,Bitmap normal, Bitmap focused)
    {
        super(normal,FOCUSABLE);
        mNormal = normal;
        mFocused = focused;
        mWidth = mNormal.getWidth();
        mHeight = mNormal.getHeight();
        this.text=text;
        setMargin(0, 0, 0, 0);
        setPadding(0, 0, 0, 0);
    }
    public void setBitmap(Bitmap bitmap)
    {
        mNormal=bitmap;

        this.bitmap=bitmap;
    }
    public void setfocusBitmap(Bitmap bitmap)
    {
        mFocused=bitmap;
    }
    public String getText()
    {
        return text;
    }
    public void setText(String text)
    {
        this.text=text;
    }
    protected void paint(Graphics graphics) {
        Bitmap bitmap = mNormal;
        if(isFocus())
        {
             bitmap = mFocused;
        }
        else
        {
            bitmap = mNormal;
        }

        graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
                        bitmap, 0, 0);
        LabelField l=new LabelField(text);

        graphics.drawText(text,  bitmap.getWidth()/2-l.getPreferredWidth()/2+3,  bitmap.getHeight()/2-l.getPreferredHeight()/2);    
    }
    protected void drawFocus(Graphics graphics, boolean on) {
    }
    protected void onFocus(int direction) {
        invalidate();
        super.onFocus(direction);
    }
    protected void onUnfocus() {
        invalidate();
        super.onUnfocus();
    }
    public int getPreferredWidth() {
        return mWidth;
    }

    public int getPreferredHeight() {
        return mHeight;
    }

    protected void layout(int width, int height) {
        setExtent(mWidth, mHeight);
    }
}