我有一个自定义编辑域
public class Custom_EditField extends EditField {
int width, row;
Custom_EditField(long style, int width, int row) {
super(style);
this.width = width;
this.row = row;
}
protected void layout(int width, int height) {
width = this.width;
height = this.row;
super.layout(width, Font.getDefault().getHeight() * row);
super.setExtent(width, Font.getDefault().getHeight() * row);
}
public int getPreferredHeight() {
return Font.getDefault().getHeight() * row;
}
public int getPreferredWidth() {
return width;
}
public void paint(Graphics graphics) {
super.paint(graphics);
graphics.setBackgroundColor(Color.GRAY);
graphics.clear();
graphics.setColor(Color.BLACK);
int labelWidth = getFont().getAdvance(getLabel());
graphics.drawRect(labelWidth, 0, getWidth() - labelWidth, getHeight());
graphics.drawText(this.getText(), 0, 0);
}
}
当我在编辑字段中输入整行单词时,会导致错误。好像不能自动转到下一行。
答案 0 :(得分:1)
BlackBerry UI中布局方法的参数是最大值,并且您的自定义代码在设置字段范围时不会尝试遵循这些最大值。这将导致您的布局出现问题。此外,paint()方法不是更改文本字段绘图的最佳位置,因为它不了解文本换行。如果要更改文本的绘制方式,但在执行包装后,您希望改写drawText。
这大概就是你想要的,但是你需要做一些调整才能让它按照你期望的方式工作:
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));
}
public int drawText(Graphics graphics,
int offset,
int length,
int x,
int y,
DrawTextParam drawTextParam) {
graphics.setBackgroundColor(Color.GRAY);
graphics.clear();
graphics.setColor(Color.BLACK);
int labelWidth = getFont().getAdvance(getLabel());
graphics.drawRect(labelWidth, 0, getWidth() - labelWidth, getHeight());
graphics.drawText(this.getText().substring(offset, offset + length), x, y);
}