2012-01-12 58 views
0

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

干杯

+0

意味着你要一个位图,其中添加一个图标,以及你必须seperately给文本........或者你必须添加一个已经有文字的图标; – alishaik786 2012-01-13 05:34:16

回答

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); 
     } 
    } } 
+0

感谢这是一个很好的溶剂,虽然它不是ButtonField它不能在Horizo​​ntalButtonFieldSet(按钮的相等间距)中正常工作。也许你有一些关于如何让几个ButtonControls靠近对方空间的指针? – DecodeGnome 2012-01-12 16:40:26

+0

Horizo​​ntalButtonFieldSet中会发生什么 – rfsk2010 2012-01-12 17:05:28

+0

如果我添加4个按钮,并且ButtonControl是最后一个ButtonControl将显示的一半(另一半在屏幕右侧)。如果我添加少于3个按钮,那么ButtonControl将根本不显示:S – DecodeGnome 2012-01-13 12:56:38