문제

BlackBerry에서 버튼의 높이와 너비를 설정할 수있는 CustomButtonfield를 만들었습니다. 문제가 직면 한 문제는 버튼 중앙에 레이블을 표시하는 방법을 모른다는 것입니다.

도움이 되었습니까?

해결책

페인트 방법을 사용하여 레이블 레이아웃, 색상 및 글꼴을 수정할 수 있습니다.
Alt Text http://img9.imageshack.us/img9/8017/custombutton.jpg
See example:

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