1

我想改变背景按钮的颜色和onfocus的文字颜色我该怎么做?

 class RoundedRectField extends Field {

    // Layout values
    private static final int CURVE_X = 12; // X-axis inset of curve
    private static final int CURVE_Y = 12; // Y-axis inset of curve
    private static final int MARGIN = 2; // Space within component boundary

    // Static colors
    private static final int TEXT_COLOR = 0xFFFFFF; // White
    private static final int BORDER_COLOR = 0xFF8000; // dark gray
    private static final int BACKGROUND_COLOR = 0xFFFFFF; // White
    private static final int TEXT_COLOR_selected = 0xFF6DB6; 
    private static final int BORDER_COLOR_selected = 0xFF8000;
    private static final int BACKGROUND_COLOR_selected = 0xCCCCCC; 
    boolean _focus = false;
    private static String text_button;
    // Point types array for rounded rectangle. Each point type
    // corresponds to one of the colors in the colors array. The
    // space marks the division between points on the top half of
    // the rectangle and those on the bottom.
    private static final byte[] PATH_POINT_TYPES = {
            Graphics.CURVEDPATH_END_POINT,
            Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT,
            Graphics.CURVEDPATH_END_POINT, Graphics.CURVEDPATH_END_POINT,
            Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT,
            Graphics.CURVEDPATH_END_POINT,

            Graphics.CURVEDPATH_END_POINT,
            Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT,
            Graphics.CURVEDPATH_END_POINT, Graphics.CURVEDPATH_END_POINT,
            Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT,
            Graphics.CURVEDPATH_END_POINT, };

    // Colors array for rounded rectangle gradient. Each color corresponds
    // to one of the points in the point types array. Top light, bottom black.
    private static final int[] PATH_GRADIENT = { 0xFF8000, 0xFF8000, 0xFF8000,
            0xFF8000, 0xFF8000, 0xFF8000,

            0xFC0500, 0xFC0500, 0xFC0500, 0xFC0500, 0xFC0500, 0xFC0500 };

    // Center our readonly field in the space we're given.
    public RoundedRectField(String text_button) {
        super(FIELD_HCENTER | FIELD_VCENTER | READONLY);
        this.text_button = text_button;
    }

    // This field in this demo has a fixed height.
    public int getPreferredHeight() {
        return 70;
    }

    // This field in this demo has a fixed width.
    public int getPreferredWidth() {
        return 240;
    }

    // When layout is requested, return our height and width.
    protected void layout(int width, int height) {
        setExtent(getPreferredWidth(), getPreferredHeight());
    }

    // When painting is requested, do it ourselves.
    protected void paint(Graphics g) {
        // Clear this area to white background, fully opaque.

            g.clear();
            g.setGlobalAlpha(255);
            g.setBackgroundColor(BACKGROUND_COLOR);

            // Drawing within our margin.
            int width = getPreferredWidth() - (MARGIN * 2);
            int height = getPreferredHeight() - (MARGIN * 2);

            // Compute paths for the rounded rectangle. The 1st point (0) is on
            // the left
            // side, right where the curve in the top left corner starts. So the
            // top left
            // corner is point 1. These points correspond to our static arrays.
            int[] xPts = { 0, 0, CURVE_X, width - CURVE_X, width, width, width,
                    width, width - CURVE_X, CURVE_X, 0, 0 };
            int[] yPts = { CURVE_Y, 0, 0, 0, 0, CURVE_Y, height - CURVE_Y,
                    height, height, height, height, height - CURVE_Y };

            // Draw the gradient fill.
            g.drawShadedFilledPath(xPts, yPts, PATH_POINT_TYPES, PATH_GRADIENT,
                    null);

            // Draw a rounded rectangle for the outline.
            // I think that drawRoundRect looks better than drawPathOutline.
            g.setColor(BORDER_COLOR);
            g.drawRoundRect(0, 0, width, height, CURVE_X * 2, CURVE_Y * 2);

            // Place some text in the center.

            Font font = Font.getDefault().derive(Font.PLAIN, 9, Ui.UNITS_pt);
            int textWidth = font.getAdvance(text_button);
            int textHeight = font.getHeight();
            g.setColor(TEXT_COLOR);
            g.setFont(font);
            g.drawText(text_button, (width / 2) - (textWidth / 2) - MARGIN,
                    (height / 2) - (textHeight / 2) - MARGIN);

    }

    protected void onFocus(int direction) {
        _focus = true;
        Dialog.alert("dcd");
        invalidate();
        super.onFocus(direction);
    }

    protected void onUnfocus() {

        _focus = false;

        invalidate();

        super.onUnfocus();
    }

}
4

1 回答 1

2

您可以通过多种方式进行操作。paint()一种流行的方法是在您已经覆盖的方法中提供自定义焦点绘图。

您应该能够做到这一点(我假设您声明_selected了聚焦状态的颜色):

if (isFocus()) {
    g.setBackgroundColor(BACKGROUND_COLOR_selected);
else {
    g.setBackgroundColor(BACKGROUND_COLOR);
}

...

if (isFocus()) {
    g.setColor(TEXT_COLOR_selected);
} else {
    g.setColor(TEXT_COLOR);
}

这些线路进入paint(),就在您当前呼叫的位置g.setBackgroundColorg.setColor(TEXT_COLOR)

然后,您将覆盖drawFocus()并且什么也不做,因为您的焦点绘图是在以下位置处理的paint()

protected void drawFocus(Graphics graphics, boolean on) {
   // override superclass implementation and do nothing
}

最后,你需要让你的Field焦点集中,以便获得焦点。你可以这样做:

public RoundedRectField(String text_button) {
    super(FIELD_HCENTER | FIELD_VCENTER | FOCUSABLE);
    this.text_button = text_button;
}

如果您需要该字段可动态聚焦(有时可聚焦,或有时不可聚焦),则可以实现此方法:

public boolean isFocusable() {

但是,如果该字段始终是可聚焦的,那么FOCUSABLE在构造函数中使用该标志将起作用。我对此进行了测试,我看到文本颜色随着焦点的变化(在 OS 5.0 9550 上)。

于 2012-07-16T09:27:46.303 回答