Blackberry - маленькая кнопка со стрелкой вверх - PullRequest
0 голосов
/ 15 декабря 2009

я хочу создать 2 кнопки для ежевики, которые выглядят так ... альтернативный текст http://img260.imageshack.us/img260/3285/button2.jpg

и второй перевернутый из вышеупомянутых

Я хотел сделать это без использования изображений (для эффективности), и кнопки должны появляться только тогда, когда на них есть фокус, и отключаться, когда фокус исчезает.

Ответы [ 2 ]

4 голосов
/ 17 декабря 2009

ArrowButtonField как расширение поля:

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 голосов
/ 16 декабря 2009

Вы можете создать собственный объект Field, а затем реализовать собственный метод paint (), который рисует нужные вам геометрические фигуры. Взгляните на класс Graphics - метод рисования вашего поля передается объекту Graphics, и вы можете использовать его для рисования заполненных прямоугольников, многоугольников и т. Д.

...