0

我将如何创建一个包含图标的自定义 ButtonField?我已经为带有自定义背景位图的 ButtonField 找到了几个不同的来源,但我想要一个带有图标和文本的按钮。我找不到有关此的任何信息,也许这里有人可以帮助我?

干杯

4

1 回答 1

1

尝试这个

package com.MYAPP.controls;


import com.MYAPP.bll.Log;

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.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;  

public class ButtonControl extends Field {
   private Bitmap selectedBitmap;
   private Bitmap unSelectedBitmap;
   private String _label; 
   protected boolean focus = false; 
   protected int bWidth = 0;
   protected int bHeight = 0;
   int padding = 0; 
   Font font=Font.getDefault();
   public ButtonControl(String icon, String label) {
       super(Field.FOCUSABLE); 
       _label = label; 
       try {


                 selectedBitmap = Bitmap.getBitmapResource(icon + "_focus.png");
                 unSelectedBitmap =Bitmap.getBitmapResource(icon + ".png");
                 padding = 0;

       } catch (Exception ex) {
               Log.Error(ex, "Cannot load icon {" + icon + "}");
       }

       bWidth = selectedBitmap.getWidth();
       bHeight = selectedBitmap.getHeight();
   }

   protected void onFocus(int direction) {
       focus = true;
       invalidate();
   }

   protected void onUnfocus() {
       focus = false;
       invalidate();
   }

   public void drawFocus(Graphics g, boolean on) {
           // do nothing... this is handled in paint...
   }

   protected int moveFocus(int amount, int status, int time) {
       // This ensures background is redrawn properly if this control is
       // the first or last in a series (eliminate white background issue)
       invalidate();
       return super.moveFocus(amount, status, time);
   }

   protected void moveFocus(int x, int y, int status, int time) {
       // This ensures background is redrawn properly if this control is
       // the first or last in a series (eliminate white background issue)
       invalidate();
       super.moveFocus(x, y, status, time);
   }

   int _labelWidth;
   int posbitmapX=0;
   int x=0;
   int y = 0;
   protected void paint(Graphics g) { 
       int oldColor = g.getColor();
       Font oldFont = g.getFont();
       g.setFont(getTextFont());  
       x = (getWidth() - bWidth) / 2;  
       if (focus) {
               g.setColor(Color.WHITE);
               g.drawBitmap(x, y, bWidth, bHeight, selectedBitmap, 0, 0);
       } else {
               g.setColor(Color.BLACK);
               g.drawBitmap(x, y, bWidth, bHeight, unSelectedBitmap, 0, 0);
       } 

       g.setFont(getTextFont());
       _labelWidth = getTextFont().getAdvance(_label);
       g.drawText(_label, 0, bHeight + 2, Graphics.HCENTER, getWidth());
       g.setColor(oldColor);
       g.setFont(oldFont);
   }


   protected void layout(int width, int height) {
           setExtent(getPreferredWidth(), getPreferredHeight());
   } 

   private Font getTextFont() { 
       return font;
   }

   public int getPreferredWidth() { 
       return selectedBitmap.getWidth();
   }

   public int getPreferredHeight() {
           return (bHeight + 2 + font.getHeight() + 2);
   }

   protected boolean keyChar(char key, int status, int time) {
       if (key == Characters.ENTER) { 
           return this.navigationClick(status, time);
       }

       return super.keyChar(key, status, time);
  } 
   protected boolean navigationClick(int status, int time) { 
       fieldChangeNotify(1); 
           return true;
  }
   protected void fieldChangeNotify(int context) {
       try {
               this.getChangeListener().fieldChanged(this, context);
       } catch (Exception e) {
               Log.Debug("Error responding to field change: " + e);
       }
   } }
于 2012-01-12T15:05:19.870 回答