Blackberry - Custom EditField Cursor
I'm creating a search box for my BlackBerry project, but it looks like the BlackBerry doesn't have an API to create a separate EditField row. I created a custom field by extending BasicEditField and overriding methods like layout and paint. In the body of the body, paint
I draw a rectangle with getPreferredWidth () and getPreferredHeight (). But the cursor is displayed at the top left position by default in the EditField. Can anyone tell me how can I draw the cursor where my text is? that is, in the middle of the EditField by calling drawText ().
a source to share
You can draw the cursor where you want to overwrite the onFocus method for the editField.
protected void onFocus(int direction) {
super.onFocus(direction);
this.setCursorPosition(this.getTextLength());
invalidate();
}
After gaining focus, the field will position the cursor at the end of the text. EditField has setCursorPosition as well as getTextLength.
Hope it helps.
a source to share
Thanks guys, I got one great solution from Blackberry Journal.
public class ScrollingSearchBox extends HorizontalFieldManager
{
private int managerWidth;
private int managerHeight;
public ScrollingSearchBox()
{
super(Manager.NO_HORIZONTAL_SCROLL);
searchEdit = new BasicEditField(){
public int getPreferredHeight()
{
return ret;
}
public int getPreferredWidth()
{
return ret;
}
public void paint(Graphics g)
{
getManager().invalidate();
super.paint(g);
}
};
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL)
{
public void sublayout(int width, int height)
{
if (managerWidth == 0) {
managerWidth = searchEdit.getPreferredWidth();
}
if (managerHeight == 0) {
managerHeight = searchEdit.getPreferredHeight();
}
super.sublayout(managerWidth, managerHeight);
setExtent(managerWidth,managerHeight);
}
public void paint(Graphics g) {
super.paint(g);
}
};
searchEdit.setMaxSize(70);
hfm.add(searchEdit);
add(hfm);
}
public int getPreferredHeight()
{
int ret = 0;
return ret;
}
protected void sublayout(int maxWidth, int maxHeight)
{
int currX = 0;
int currY = 0;
Field currField;
currField = this.getField(0);
switch (ScreenConfig.getInstance().getScreen())
{
case ScreenConfig.SCREEN_320_240:
currX = 5;
currY = 3;
break;
case ScreenConfig.SCREEN_480_360:
case ScreenConfig.SCREEN_480_320:
currX = 5;
currY = 1;
break;
}
this.setPositionChild(currField, currX, currY);
this.layoutChild(currField, currField.getPreferredWidth(),
currField.getPreferredHeight());
setExtent(this.getPreferredWidth(), this.getPreferredHeight());
}
protected void paint(Graphics graphics)
{
super.paint(graphics);
graphics.drawRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
}
}
I am giving this to help others. Keep sharing your codes. Thanks.
a source to share