光标未出现在Editfield ..
以下是代码..
ef_mob = new EditField("", "", 11, EditField.NO_NEWLINE|EditField.FILTER_NUMERIC|EditField.FOCUSABLE) {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
// graphics.drawRoundRect(0, 0, getWidth()-(getWidth()/10), getHeight(), 5, 5);
graphics.fillRoundRect(0, 0, getWidth()-(getWidth()/10), getHeight(), 5, 5);
graphics.setColor(Color.BLACK);
graphics.drawText(ef_mob.getText(), 0, 0);
super.paint(graphics);
}
};
有什么问题?
答案 0 :(得分:4)
仅仅在EditField
上正确绘制圆角矩形背景真是太愚蠢了,但它看起来像others have had this problem, too
如果您调整Peter Strange对该问题的回答,您的代码可能看起来像下面发布的MobEditField
。我将您的匿名EditField
类拆分为一个单独的类,因为添加了很多代码。如果你真的想要,你可以将代码保持为匿名类(我认为这对可读性非常不利)。
private class MobEditField extends EditField {
private boolean _drawFocus = false;
public MobEditField() {
super("", "", 11, EditField.NO_NEWLINE|EditField.FILTER_NUMERIC|EditField.FOCUSABLE);
}
protected void paint(Graphics graphics) {
if (!_drawFocus) {
int oldColor = graphics.getColor();
graphics.setColor(Color.WHITE);
graphics.fillRoundRect(0, 0, getWidth() - (getWidth() / 10), getHeight(), 5, 5);
graphics.setColor(Color.BLACK);
graphics.drawText(getText(), 0, 0);
graphics.setColor(oldColor);
}
super.paint(graphics);
}
protected void drawFocus(Graphics graphics, boolean on) {
_drawFocus = on;
super.drawFocus(graphics, on);
_drawFocus = false;
}
protected void onFocus( int direction ) {
super.onFocus( direction );
invalidate();
}
protected void onUnfocus() {
super.onUnfocus();
invalidate();
}
}
然后你就做了:
ef_mob = new MobEditField();
请注意以下几点:
ef_mob.getText()
方法中调用paint()
。只需致电getText()
即可。我不知道你的原始代码是如何编译的。paint()
方法时,保存初始Graphics
对象颜色(或alpha,或任何更改的内容),然后在最后重置它。onFocus()
和onUnfocus()
方法以强制重新绘制paint()
方法只允许super.paint()
在关注字段时完成所有工作。根据您希望场的外观,您可能需要稍微调整一下。另一个对我有用的选择是利用paintBackground()
具有的无证件 EditField
方法。当然,使用未记录的方法总是存在缺点。所以,我只提供它作为选项:
ef_mob = new EditField("", "", 11, EditField.NO_NEWLINE|EditField.FILTER_NUMERIC|EditField.FOCUSABLE) {
protected void paint(Graphics graphics) {
int oldColor = graphics.getColor();
graphics.setColor(Color.BLACK);
graphics.drawText(getText(), 0, 0);
graphics.setColor(oldColor);
super.paint(graphics);
}
protected void paintBackground(Graphics g) {
int oldColor = g.getColor();
g.setColor(Color.WHITE);
g.fillRoundRect(0, 0, getWidth()-(getWidth()/10), getHeight(), 5, 5);
g.setColor(oldColor);
}
};
在此,我将fillRoundRect()
来自paint()
并移至paintBackground()
。