I took one custom text box image, in that text box I want to show "Enter pa开发者_如何学运维ssword" and when I focus on to that text box field and enter any charectors it converts to asterisk symbols like password edit field.
I want like this below Image:
I think it is possible by only overriding paint(..)
of the Editfield
. May be following steps can achieve the desired behaviour:
- Store original text to a local variable.
- Generate fake text depending on focus status, original text etc.
- Call
super.paint(..)
, it will paint the field with fake text. - Use
setText(..)
to set original text back.
Draft implementation:
class MyEditField extends EditField {
private final static String INIT_TEXT = "Enter Password";
protected void paint(Graphics graphics) {
String originalText = getText();
String fakeText = originalText;
if (originalText.equalsIgnoreCase("")) {
fakeText = isFocus() ? originalText : INIT_TEXT;
} else {
fakeText = "";
for (int i=0;i<originalText.length();i++) {
fakeText += '*';
}
}
setText(fakeText);
super.paint(graphics);
setText(originalText);
}
}
Above code doesn't paint a border.
We need to set a monochrome font for the problem you have mentioned in comment. Here is a sample code:
MyEditField me = new MyEditField();
try {
Font font = null;
FontFamily fontFamily[] = FontFamily.getFontFamilies();
for (int i=0;i<fontFamily.length; i++) {
if (fontFamily[i].getName().equalsIgnoreCase("Courier New")) {
font = fontFamily[i].getFont(FontFamily.SCALABLE_FONT,
Font.getDefault().getHeight());
me.setFont(font);
break;
}
}
} catch (Exception e) {
}
If I understand you correctly, you need to use a PasswordEditField.
Implement it just like any LabelField, and it will do what you need.
I see you wanted the background text too - I would use drawText() from within the paint() method to draw that. The other answer that shows a full implementation by using EditField is also good.
精彩评论