Reputation: 69
I m trying to add a default Label to searchField when the user put a text "search:" get ereased. I tried this
editedField.setLabel("search:")
But it doesn't work because when I put a string search don't erase. For exemple If I put "a" it displays searcha. Could you plz help me? thanx a lot.
Upvotes: 2
Views: 73
Reputation: 4158
try this -
private String test="";
final EditField email_edit = new EditField("", "", 30,BasicEditField.FILTER_DEFAULT) {
String emptyString = "Search";
protected void paint(Graphics g) {
int oldColor = g.getColor();
try {
g.setColor(0x959595);
test = super.getText();
if ( test == null || test.length() < 1 ) {
g.drawText(emptyString, 0, 0);
}
super.paint(g);
} finally {
g.setColor(oldColor);
}
}
};
Upvotes: 1
Reputation: 512
Basically, you need a hint in your TextField
.
You need to override its paint
method , You can use this code:
protected void paint(Graphics graphics) {
if (beditfield.getText().length() == 0)
{
graphics.setColor(Color.GRAY);
graphics.drawText("Search", 0, 0);
}
graphics.setColor(Color.WHITE);
invalidate();
super.paint(graphics);
}
It checks the textfield length. When it is zero, it draws the hint there.
Upvotes: 3
Reputation:
Override onFocus method and when field is focused, then clear its contents.
To set text field contents use setText method, instead of setLabel
Upvotes: 1