i am pretty new to the gwt framework and i am using it for building the ui of my web site, i would like to make the text box 开发者_StackOverflow社区have a text in it that once the user clicks on it for the first time, the text disappears. and in the rest of the time it behaves like a normal text box
any ideas on how to do it?
When you create the textbox, set the default text and add a keyboard listener:
TextBox box = new TextBox();
box.setText("Default Text");
box.addKeyboardListener(this);
defaultValue = true; // this is a global boolean value
Then have your class implement KeyboardListener leaving them all blank except:
public void onKeyPress(Widget arg0, char arg1, int arg2)
{
if(defaultValue)
{
box.setText = "";
defaultValue = false;
}
}
you can add a clickHandler
to the box.
Within the handler you do something as easy as:
if(text==DEFAULT_TEXT)
{
text==""
}
If someone is going to write again the same DEFAULT_TEXT
it would get wiped out again.
If you want to avoid that add boolean variable in the check expression.
Can't tell it for GWT, but a general approach could be:
- use a variable to flag, whether the text box is 'initialized' or 'in use'
- add a listener to the text widget (I'd use a KeyboardListener and make the text disappear when the user starts entering text and not on the first - maybe accidental - mouse click)
- When the listener receives the first event for the widget (flag = 'initialized'), clear the flag and replace the text inside the text field with the actual keystroke.
(for a click listener: upon the first click on the widget clear the flag and the text box.)
精彩评论