Question

I built custom edit field because i want to change the background color.

first, second and third shown unstable, sometime display when focusing, disappear when lost focusing.

I want the result like third image where the focusing at where also static display all the field.

Here is my Custom_EditField:

public class Custom_EditField extends EditField {
private int width, row, color;
private boolean isfocus;

Custom_EditField(long style, int width, int row) {
    super(style);
    this.width = width;
    this.row = row;
}

public int getPreferredHeight() {
    return Font.getDefault().getHeight() * row;
}

public int getPreferredWidth() {
    return width;
}

protected void onFocus(int direction) {
    color = Color.GRAY;
    isfocus = true;
}

protected void onUnfocus() {
    color = Color.GOLD;
    isfocus = false;
}

protected void layout(int maxWidth, int maxHeight) {
    super.layout(maxWidth,
            Math.min(maxHeight, Font.getDefault().getHeight() * row));
    super.setExtent(maxWidth,
            Math.min(maxHeight, Font.getDefault().getHeight() * row));
}

protected void paint(Graphics graphics) {
    int rectHeight = getPreferredHeight();
    int rectWidth = getPreferredWidth();
    try {
        if (isfocus) {
            graphics.setBackgroundColor(color);
        } else {
            graphics.setBackgroundColor(color);
        }
        color = Color.BLACK;
        graphics.setColor(color);
        graphics.drawRect(0, 0, rectWidth, rectHeight);
        super.paint(graphics);
    } finally {
        graphics.setColor(color);
    }
}
}
Was it helpful?

Solution

I'm not 100% sure which problem you're asking about:

  • how to draw your own focus background colors for an EditField, or
  • how to fix the problem with fields disappearing as you move focus

1) If it's the second problem (disappearing), then I would guess that you're having the same problem as in your other question, with the Custom_TopField buttons disappearing

So, if these Custom_EditField objects are created by a manager that extends VerticalFieldManager, but also implements sublayout() itself to perform all the positioning, then try the solution I suggested in the other question (don't extend VerticalFieldManager, just extend Manager).

2) If that doesn't work, it's possible that your paint() method is not getting triggered when it should. Try adding a call to invalidate() in your focus methods, along with a call to the superclass methods onFocus() and onUnfocus():

protected void onFocus(int direction) { 
    color = Color.GRAY; 
    isfocus = true; 
    invalidate();
    super.onFocus(direction);
} 

protected void onUnfocus() { 
    color = Color.GOLD; 
    isfocus = false; 
    invalidate();
    super.onUnfocus();
} 

3) And, you might need to implement this, too:

public boolean isFocusable() {
    return true;
}

But, first, check your manager class, to be sure this isn't the same problem as in your other question.

Also ...

In this class, you are basing the height of the edit field on the row number. Is this really what you want? Row 0 would be 0 size, and each row after that would get taller and taller. Is that really the UI? Normally, you would have all the rows' edit fields be the same size, and simply lay them out with a different y offset. Most likely, this class should not need to know which row it has been placed on. That information is normally the responsibilty of a manager object.

OTHER TIPS

protected EditField getEditField() {

    EditField edit_field = new EditField("", "", 10, EditField.NO_NEWLINE
            | BasicEditField.FIELD_VCENTER) {


        protected boolean navigationClick(int status, int time) {

        }

        protected void onFocus(int direction) {
            Set the boolean during focus
        }

        protected void onUnfocus() {

        }

        protected void paintBackground(Graphics graphics_paint) {
            if(that boolean is true)
                  {
                       Set your custom background during focus.
                  }else{
                       Set your custom background during unfocus.
                  }
        }

        protected void paint(Graphics graphics_paint) {
            int old_color = graphics_paint.getColor();
            try {
                if(that boolean is true)
                  {
                       Set your custom Font color during any event like focus and click.
                  }else{
                       Set your custom background Font color during any event like unfocus.
                  }
                super.paint(graphics_paint);
            } finally {
                graphics_paint.setColor(old_color);
            }
        }

        public void layout(int width, int height) {

        }
    };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top