2009-12-15 62 views

回答

4

ArrowButtonField作为现场extention:

class ArrowButtonField extends Field { 
    public static final int TYPE_UP = 0; 
    public static final int TYPE_DOWN = 1; 

    private int mBackgroundColor = Color.WHITE; 
    private int mFillColor = Color.CRIMSON; 
    private int mWidth = 20; 
    private int mHeight = 12; 
    private int mArrowType = TYPE_UP; 

    public ArrowButtonField(int bgColor, int fillColor, int arrowType) { 
     super(FOCUSABLE); 
     setMargin(0, 0, 0, 0); 
     setPadding(0, 0, 0, 0); 
     mArrowType = arrowType; 
     mBackgroundColor = bgColor; 
     mFillColor = fillColor; 
    } 

    // cancel theme border and background style 
    protected void applyTheme(Graphics arg0, boolean arg1) { 
    } 

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

    protected void onUnfocus() { 
     invalidate(); 
     super.onUnfocus(); 
    } 

    protected void paint(Graphics graphics) { 
     graphics.clear(); 
     graphics.setColor(mBackgroundColor); 
     graphics.fillRect(0, 0, mWidth, mHeight); 
     if (isFocus()) { 
      graphics.setColor(mFillColor); 
      int xc = 10; 
      int y1 = 0, y2 = 0, x2 = xc - 9, x1 = xc + 9; 

      switch (mArrowType) { 
      case TYPE_DOWN: 
       y1 = 11; 
       y2 = 1; 
       break; 
      case TYPE_UP: 
       y1 = 1; 
       y2 = 11; 
       break; 
      default: 
       break; 
      } 
      int[] xPts = new int[] { x1, x2, xc }; 
      int[] yPts = new int[] { y1, y1, y2 }; 
      graphics.drawFilledPath(xPts, yPts, null, null); 
     } 
    } 

    public int getPreferredWidth() { 
     return mWidth; 
    } 

    public int getPreferredHeight() { 
     return mHeight; 
    } 

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

为向上和向下箭头类:

class UpArrowButtonField extends ArrowButtonField { 
    public UpArrowButtonField(int backgroundColor, int fillColor) { 
     super(backgroundColor, fillColor, TYPE_UP); 
    } 
} 

class DownArrowButtonField extends ArrowButtonField { 
    public DownArrowButtonField(int backgroundColor, int fillColor) { 
     super(backgroundColor, fillColor, TYPE_DOWN); 
    } 
} 

使用的样本:

class Scr extends MainScreen implements FieldChangeListener { 
    UpArrowButtonField arrowUp; 
    DownArrowButtonField arrowDown; 

    public Scr() { 
     arrowUp = new UpArrowButtonField(Color.WHITE, Color.RED); 
     arrowUp.setChangeListener(this); 
     add(arrowUp); 
     arrowDown = new DownArrowButtonField(Color.WHITE, Color.RED); 
     arrowDown.setChangeListener(this); 
     add(arrowDown); 
    } 

    public void fieldChanged(Field field, int context) { 
     if (field == arrowUp) { 
      Dialog.inform("UP"); 
     } else if (field == arrowDown) { 
      Dialog.inform("DOWN"); 
     } 
    } 
} 
0

您可以创建自定义Field对象,然后实现绘制几何形状的自定义paint()方法你需要。看看在Graphics类 - 你的领域的paint方法传递Graphics对象,你可以利用该绘制填充矩形,多边形等