10-11-2011 07:22 PM
Hello,
I am new in BB dev. Trying to write small program with two CustomTextFields, but faceing with the problem then trying to check which CustomTextField is selected (focused).
CustomTextField (NumericTextField)
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Font;
public class NumericTextField extends Manager
{
private final static int DEFAULT_LEFT_MARGIN = 10;
private final static int DEFAULT_RIGHT_MARGIN = 10;
private final static int DEFAULT_TOP_MARGIN = 5;
private final static int DEFAULT_BOTTOM_MARGIN = 5;
private final static int DEFAULT_LEFT_PADDING = 10;
private final static int DEFAULT_RIGHT_PADDING = 10;
private final static int DEFAULT_TOP_PADDING = 5;
private final static int DEFAULT_BOTTOM_PADDING = 5;
private int topMargin = DEFAULT_TOP_MARGIN;
private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
private int leftMargin = DEFAULT_LEFT_MARGIN;
private int rightMargin = DEFAULT_RIGHT_MARGIN;
private int topPadding = DEFAULT_TOP_PADDING;
private int bottomPadding = DEFAULT_BOTTOM_PADDING;
private int leftPadding = DEFAULT_LEFT_PADDING;
private int rightPadding = DEFAULT_RIGHT_PADDING;
private int totalHorizontalEmptySpace = leftMargin + leftPadding + rightPadding + rightMargin;
private int totalVerticalEmptySpace = topMargin + topPadding + bottomPadding + bottomMargin;
private int minHeight = getFont().getHeight() + totalVerticalEmptySpace;
private int width = Display.getWidth();
private int height = minHeight;
private EditField editField;
public NumericTextField()
{
super(0);
editField = new EditField(EditField.FILTER_REAL_NUMERIC);
add(editField);
}
protected void sublayout(int width, int height)
{
Field field = getField(0);
layoutChild(field, this.width - totalHorizontalEmptySpace, this.height - totalVerticalEmptySpace);
setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);
setExtent(this.width, this.height);
}
public void setTopMargin(int topMargin)
{
this.topMargin = topMargin;
}
public void setBottomMargin(int bottomMargin)
{
this.bottomMargin = bottomMargin;
}
protected void paint(Graphics graphics)
{
graphics.drawRoundRect(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), 5, 5);
boolean longText = false;
EditField ef = (EditField)getField(0);
String entireText = ef.getText();
String textToDraw = "";
Font font = getFont();
int availableWidth = width - totalHorizontalEmptySpace;
if (font.getAdvance(entireText) <= availableWidth)
{
textToDraw = entireText;
}
else
{
int endIndex = entireText.length();
for (int beginIndex = 1; beginIndex < endIndex; beginIndex++)
{
textToDraw = entireText.substring(beginIndex);
if (font.getAdvance(textToDraw) <= availableWidth)
{
longText = true;
break;
}
}
}
if (longText == true)
{
ef.setText(textToDraw);
super.paint(graphics);
ef.setText(entireText);
}
else
{
super.paint(graphics);
}
}
public int getPreferredWidth()
{
return width;
}
public int getPreferredHeight()
{
return height;
}
protected boolean keyChar(char ch, int status, int time)
{
if (ch == Characters.ENTER)
{
return true;
}
else
{
return super.keyChar(ch, status, time);
}
}
public String getText()
{
return ((EditField)getField(0)).getText();
}
public void setText(final String text)
{
((EditField)getField(0)).setText(text);
}
}
and in my content screen trying to check:
NumericTextField focusImput() {
NumericTextField focusField;
if (_fieldFrom.isFocus()) {
focusField = _fieldtFrom;
}
else {
focusField = _fieldTo;
}
return focusField;
}but it always returns me _fieldTo...
defining NumericTextField:
_fieldFrom = new NumericTextField(); _fieldFrom.setChangeListener(this); manager.add(_fieldFrom); _fieldTo = new NumericTextField(); _fieldTo.setChangeListener(this); manager.add(_fieldTo);
Could maybe anybody to guide me how to achieve correct focus functionality?
Thanks in advance.
Solved! Go to Solution.
10-11-2011 08:09 PM
You could try overiding your Managers isFocus with something like:
public boolean isFocus() {
return ((EditField)getField(0)).isFocus();
}
10-11-2011 08:43 PM
thanks peter for the fast replay, I will try it and inform you. Thanks again.
10-12-2011 04:02 PM
this option not working, Getting an exception: Uncaught: ClassCastException
Any others options?
10-12-2011 04:22 PM
Wrong Manager - put that isFocus override inside your NumericTextField class body.
You've got at least one problem in your NumericTextField - if your text is long (longText is true), you are going to endlessly paint and re-paint your NumericTextField. setText on EditField invokes updateLayout() which causes the screen to re-layout and re-paint. See the problem?
Overall, you probably don't need such a complicated class. What exactly are you trying to achieve?
10-12-2011 05:06 PM
yes I put
public boolean isFocus(){
return ((EditField)getField(0)).isFocus();
}in my NumericTextField class, and checking from contextscreeen if numericField.isFocus, but nothing happens...
As I mention in the begining, that I am new in BB development (and this code is far from perfect), I would verry apreciate, if you could explain in more detail with provided example.
I am trying to do 2 textboxes that allow real numbers (positive and negative with / without '.') and basic math between them, also have one combobox with choice which allows to select if one othe textbox becomes as a constant or not (not editible). This is basic things what I trying to achieve. Now stucked with the checking which NumericTextField is focused or selected.
Maybe you are right that this is too complex (I tried to reuse this NumericTextField class that I have found in this forum) and adjust to my needs.
Thanks in advance for your help.
10-13-2011 01:13 AM
Then you need two slightly custom EditFields - their main customization would be in limiting their width, plus making them focusable/unfocusable. So something like this should work:
public class NumericTextField extends EditField {
// override the constructors used in your code, adding FILTER_REAL_NUMERIC
// ...
private int maxWidth = Integer.MAX_VALUE >> 1;
private boolean focusable = true;
protected void layout(int width, int height) {
super.layout(Math.min(maxWidth, width), height);
}
public void setMaxWidth(int width) {
maxWidth = width;
updateLayout();
}
public boolean isFocusable() {
return (isStyle(FOCUSABLE) & focusable);
}
public void setFocusable(boolean on) {
focusable = on;
}
}
If you want to position the fields, use setMargin - it is documented only in OS 6.0, but has been working since OS 4.2.
Use setMaxWidth to, well, set maximum width desired (otherwise EditField gobbles up all available width). Use setFocusable to enable/disable the field. In addition, you might want to play with visual states of the field as you enable/disable it, but that's an exersize for another day. First make sure this is working and you know how to use it.
10-15-2011 06:36 PM - last edited on 10-15-2011 08:33 PM
Thanks for this, could you tell how I could paint borders around NumericTextField?, and where I could set that my NumericTextField will only accept real number (negative and positive).
Very appreciate for your help.
Thanks in advance.
10-15-2011 09:01 PM
FILTER_REAL_NUMERIC is your friend for input limitations (see how the EditField is created inside the code you've used before).
As for borders - do you need to support OS older than 4.6? If not, any field has setBorder method (see net.rim.device.api.ui.Field documentation). There are quite a few overloads of that call, but the simplest one is sufficient unless you want to change colors and/or texture along with focus changes. If you do, state that and I'll help you further. I just don't want to confuse you with too many details unless I absolutely have to ![]()
10-15-2011 10:20 PM
Thanks a lot, I am not going support older than 4.6. I will look more into documentation and if I will have any questions (sure that I will have) I will ask.
Thanks again.