09-10-2012 02:46 AM - edited 09-10-2012 02:49 AM
I have a Basiceditfield with initial text. Following is the code:
searchField = new EditField("","",1024,EditField.FILTER_DEFAULT|Edit
{
protected void onFocus(int direction)
{
setLabel("");
this.invalidate();
};
protected void onUnfocus()
{
setLabel("Search");
this.invalidate();
};
};
searchField.setLabel("Search");
Initialy text in searchfield is"Search". Onfocus its set to "". I type something in searchfield ,suppose "ab".When focus is removed from searchfield label is set to "Searchab". whereas i want it as "Search". Please help.
Solved! Go to Solution.
09-10-2012 02:53 AM
in onUnfocus, first clear the text before setting the label.
setText("");
setLabel("Search");
09-10-2012 03:09 AM - edited 09-10-2012 03:11 AM
I want following things to be done:
1.When i do not type anything in searchfield and if i focus and unfocus searchfield, then Initially text should be "Search". Onfocus text should be blank. Onunfocus text should be "Search".
2.When i type text in searchfield, and remove focus then text in editfield should be typed text only.
3.After condition 2 ,when i focus editfield and deleted all the text which i have type and remove the focus then text should b set to "Search". And searching should not get start with "Search".
Please help me out.
09-10-2012 03:33 AM
09-10-2012 03:39 AM
You may try to override paint method as per your need (see sample below).
protected void paint(Graphics graphics) {
super.paint(graphics);
if (!hasFocus) {
if (editField.getText() == null || editField.getText().equals("")
|| editField.getText().trim().length() == 0) {
editField.setText("");
// if EditField is Empty Then it will show initial String
graphics.setColor(Color.GRAY);
int y = (height - editField.getFont().getHeight()) / 2;
graphics.drawText(initial, 10, y + 1);
graphics.setColor(Color.BLACK);
}
}
09-10-2012 03:58 AM
I am agree with simon_hain.
09-10-2012 04:23 AM - edited 09-10-2012 04:26 AM
simon hain:
I did the changes as per your suggestion:
searchField = new EditField("","",1024,EditField.FILTER_DEFAULT|Edit
{
protected void onFocus(int direction)
{
if(getText() == "Search")
setText("");
this.invalidate();
};
protected void onUnfocus()
{
if(getText() == "")
setText("Search");
this.invalidate();
};
protected boolean keyChar(char key, int status, int time)
{
if(this.getText().equals("Search"))
this.setText("");
return super.keyChar(key, status, time);
}
};
searchField.setText("Search");
I am facing following problems:
a.Unable to set text to "" on focus.
b.Unable to do thing specified in condition 3 as i mentioned above.
c.When i click on searchfield it is not set to "".
09-10-2012 04:36 AM