【问题标题】:Blackberry custom ButtonField with icon and text带有图标和文本的黑莓自定义 ButtonField
【发布时间】:2012-01-12 14:58:48
【问题描述】:

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

干杯

【问题讨论】:

  • 意味着您想要一个添加图标的位图,并且您必须单独提供文本........或者您必须添加一个已经有文本的图标;

标签: blackberry buttonfield


【解决方案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);
       }
   } }

【讨论】:

  • 谢谢,这是一个很好的解决方案,尽管由于它不是 ButtonField,它在 Horizo​​ntalButtonFieldSet 中无法正常工作(对于等间距的按钮)。也许你有一些关于如何让几个 ButtonControls 平等地相邻的指针?
  • Horizo​​ntalButtonFieldSet 中发生了什么
  • 如果我添加 4 个按钮,ButtonControl 是最后一个,那么 ButtonControl 的一半将显示(另一半在屏幕右侧)。如果我添加的按钮少于 3 个,则 ButtonControl 根本不会显示:S
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多