سؤال

لقد قمت بإنشاء CustomButtonField في بلاك بيري باستخدام أنه يمكنني ضبط ارتفاع الزر وعرضه. المشكلة التي أواجهها هي أنني لا أعرف كيفية إظهار الملصق في وسط الزر.

هل كانت مفيدة؟

المحلول

يمكنك استخدام طريقة الطلاء لتعديل تخطيط الملصق ولونه وخطه.
نص بديل http://img9.imageshack.us/img9/8017/custombutton.jpg
انظر المثال:

class CustomButton extends ButtonField {
    int mHeight;
    int mWidth;

    public CustomButton(int height, int width, String label) {
        super(label, CONSUME_CLICK);
        mHeight = height;
        mWidth = width;
    }

    public int getPreferredHeight() {
        return mHeight;
    }

    public int getPreferredWidth() {
        return mWidth;
    }

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

    protected void paint(Graphics graphics) {
        graphics.setColor(Color.WHITE);
        String label = getLabel();
        int x = (getPreferredWidth() - getFont().getAdvance(label)) >> 1;
        int y = (getPreferredHeight() - getFont().getHeight()) >> 1;
        graphics.drawText(label, x, y);
    }
}

مثال للاستخدام:

class Scr extends MainScreen implements FieldChangeListener {
    CustomButton button1;
    CustomButton button2;
    CustomButton button3;

    public Scr() {
        add(button1 = new CustomButton(15, 60, "first"));
        button1.setChangeListener(this);
        add(button2 = new CustomButton(30, 120, "second"));
        button2.setChangeListener(this);
        add(button3 = new CustomButton(50, 200, "third"));
        button3.setChangeListener(this);
    }

    public void fieldChanged(Field field, int context) {
        if (field == button1)
            Dialog.inform("first");
        if (field == button2)
            Dialog.inform("second");
        if (field == button3)
            Dialog.inform("third");
    }

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top