黑莓 - 小向上箭头按钮

时间:2009-12-15 19:47:12

标签: user-interface graphics blackberry custom-controls

我想为黑莓创建2个按钮,看起来像这样...... alt text http://img260.imageshack.us/img260/3285/button2.jpg

和第二个反转上述

我想在不使用图像的情况下这样做(为了提高效率),只有当焦点对焦时才会出现按钮,当焦点消失时按钮应该出现。

2 个答案:

答案 0 :(得分:4)

箭头ButtonField作为Field扩展名:

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");
        }
    }
}

答案 1 :(得分:0)

您可以创建自定义Field对象,然后实现绘制所需几何形状的自定义paint()方法。看一下Graphics类 - 你的字段的paint方法被传递给Graphics对象,你可以利用它来绘制填充的矩形,多边形等。