如何动态更改黑莓标签字段的字体颜色?

5
我有一个标签字段和三个名为红色、黄色、蓝色的按钮。如果我点击红色按钮,则标签字段的字体颜色应更改为红色;同样,如果我点击黄色按钮,则字体颜色应更改为黄色;同理,根据按钮颜色,在标签字段中更改字体颜色。请问有人知道如何做到这一点吗?
1个回答

13

在LabelField中设置字体颜色可以通过在super.paint之前的paint事件中设置graphics.setColor来轻松维护:

    class FCLabelField extends LabelField {
        public FCLabelField(Object text, long style) {
            super(text, style);
        }

        private int mFontColor = -1;

        public void setFontColor(int fontColor) {
            mFontColor = fontColor;
        }

        protected void paint(Graphics graphics) {
            if (-1 != mFontColor)
                graphics.setColor(mFontColor);
            super.paint(graphics);
        }
    }

    class Scr extends MainScreen implements FieldChangeListener {
        FCLabelField mLabel;
        ButtonField mRedButton;
        ButtonField mGreenButton;
        ButtonField mBlueButton;

        public Scr() {
            mLabel = new FCLabelField("COLOR LABEL", 
                    FIELD_HCENTER);
            add(mLabel);
            mRedButton = new ButtonField("RED", 
                    ButtonField.CONSUME_CLICK|FIELD_HCENTER);
            mRedButton.setChangeListener(this);
            add(mRedButton);
            mGreenButton = new ButtonField("GREEN", 
                    ButtonField.CONSUME_CLICK|FIELD_HCENTER);
            mGreenButton.setChangeListener(this);
            add(mGreenButton);
            mBlueButton = new ButtonField("BLUE", 
                    ButtonField.CONSUME_CLICK|FIELD_HCENTER);
            mBlueButton.setChangeListener(this);
            add(mBlueButton);
        }

        public void fieldChanged(Field field, int context) {
            if (field == mRedButton) {
                mLabel.setFontColor(Color.RED);
            } else if (field == mGreenButton) {
                mLabel.setFontColor(Color.GREEN);
            } else if (field == mBlueButton) {
                mLabel.setFontColor(Color.BLUE);
            }
            invalidate();
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接