自定义编辑字段不稳定

时间:2012-07-05 03:20:29

标签: blackberry focus blackberry-editfield

我构建了自定义编辑字段,因为我想更改背景颜色。

firstsecondthird显示不稳定,有时会在对焦时显示,在失去对焦时会消失。

我希望结果像第三张图像,其中聚焦在哪里也静态显示所有场。

这是我的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);
    }
}
}

2 个答案:

答案 0 :(得分:1)

我不是100%确定你问的是哪个问题:

  • 如何为EditField
  • 绘制自己的焦点背景颜色
  • 如何解决移动焦点时字段消失的问题

1)如果这是第二个问题(消失),那么我猜你的问题与其他问题相同,Custom_TopField {{3} }

因此,如果这些Custom_EditField对象是由扩展VerticalFieldManager的管理器创建的,但也实现了sublayout()本身来执行所有定位,那么请尝试我在另一个中建议的解决方案问题(不要扩展VerticalFieldManager,只需扩展Manager)。

2) 如果这不起作用,则可能无法触发paint()方法。尝试在焦点方法中添加对invalidate()的调用,同时调用超类方法onFocus()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)而且,您可能也需要实现这一点:

public boolean isFocusable() {
    return true;
}

但是,首先,请检查您的经理课程,以确保这与您的其他问题不同。

另外......

在此课程中,您将编辑字段的高度基于行号。这真的是你想要的吗?第0行将是0大小,之后的每一行将变得更高和更高。这真的是用户界面吗?通常情况下,您将使所有行的编辑字段具有相同的大小,并使用不同的 y偏移进行简单布局。最有可能的是,这个班级不应该知道它被放置了哪个row。该信息通常是经理对象的责任。

答案 1 :(得分:0)

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) {

        }
    };