Problem with retention of component focus

76 views
Skip to first unread message

CS

unread,
Mar 23, 2014, 10:37:37 AM3/23/14
to codenameone...@googlegroups.com
Dear CNO team,

I am trying to validate what user inputs into textfield. I decided to do the a check with focus listener, since as far as I tested is the best way (it catches all possible scenarios of leaving textfield component). So I do a check on focusLost(). And if the validation is unsuccessful a dialog with notification is shown and cmp.requestFocus() called to "prevent" leaving the component. But in some scenarios this does not work as expected.
E.g.: I have three textfield components. On first textfield I have put a check on focusLost(). If I insert invalid text and press "Next" on the VK, I get a notification dialog as expected, but after I close it I can see two cursors (one blinking in first textfield and second blinking in second field). If I press a key on VK, the chars are outputted to the second textfield and also the "Enter" button is not "Next" (since there is one more textfield) but "Done". If I press "Done" I am returned to the first textfield and VK is closed. This was tested on Android.
I hope you can replicate and fix this bug.

If there is a better/proper way for checking validation of input … please let me know.

Regards,
CS

Shai Almog

unread,
Mar 23, 2014, 3:04:49 PM3/23/14
to codenameone...@googlegroups.com, computer.s...@gmail.com
Hi,
generally editing is a bit more complicated than that since you have both the focus and the native editing happening at once.
What might work for this case is also a call to Display.editString() to return to the old editing component in case you are moving to a text area.

CS

unread,
Mar 24, 2014, 4:25:25 PM3/24/14
to codenameone...@googlegroups.com
Though calling Display.editString() did fix the initial position of the cursor after pressing "Next" (it was blinking only in first textfield), but it caused some other problems. If I press "Next" one more time, the text in "first" textfield is changed to "initial" value (before I applied changes, which caused invalid value) and another cursor appears in third textfield. So now again, two cursors blink in first and third textfield. Pressing chars on VK adds them to third textfield.

If I remove requestFocus from lostFocus function on second "Next" press the lostFocus is no more detected (meaning invalid value is accepted) and the cursor moves to third textfield (skipping second). So this is also not a solution.

To some degree I am surprised that it seems that nobody had this problem so far.

I hope you will be able to find some "proper" solution in the future.

Shai Almog

unread,
Mar 25, 2014, 1:58:04 AM3/25/14
to codenameone...@googlegroups.com, computer.s...@gmail.com
I suggest you wrap the call to the editString in a callSerially to make sure the previous editing gets flushed.

CS

unread,
Mar 25, 2014, 2:14:43 PM3/25/14
to codenameone...@googlegroups.com
Unfortunately this did not help much. It seems a little better, but I still get to "two cursors" after two "next"-s, when I get to "done".

The problem is also if I touch outside the text field. The first time I do that I am returned to the "non-valid" text field as expected, if I repeat this the second time, I am allowed out of text field and the value is "returned" to previous valid value. But what is even "stranger" is that this does not always happen, sometimes I have to press many times outside the validated text field to get this "response".

Btw. on simulator this works as expected, I cannot leave textfield until the text is valid.

Thanks for all the help.

Shai Almog

unread,
Mar 25, 2014, 3:25:11 PM3/25/14
to codenameone...@googlegroups.com, computer.s...@gmail.com
On which device type are you experiencing this?
If this is in iOS are you using the ios.keyboardOpen=true flag?

CS

unread,
Mar 26, 2014, 9:22:47 AM3/26/14
to codenameone...@googlegroups.com
Tested on Android:
- 4.4.2 (HTC)
- 4.2.2 (CM)

Shai Almog

unread,
Mar 26, 2014, 2:26:04 PM3/26/14
to codenameone...@googlegroups.com, computer.s...@gmail.com
You can probably do something with highlights to indicate validation but generally this should work. Maybe Chen has a better idea on why this isn't doing what you'd expect on Android.

Chen Fishbein

unread,
Mar 27, 2014, 4:59:26 AM3/27/14
to codenameone...@googlegroups.com, computer.s...@gmail.com
I'm not sure what you are trying to do and what is not working, can you please share some code?

CS

unread,
Mar 27, 2014, 12:58:16 PM3/27/14
to codenameone...@googlegroups.com
Sure.

textField.addFocusListener(new FocusListener() {
    public void focusGained(Component cmp) {}

    public void focusLost(final Component cmp) {
        // check if input is valid, e.g. is shorter than 5 chars, if not then
        if (!(((TextField) cmp).getText().length() < 5)) { 
            // show dialog to the user
            Dialog.show("Error", "Max length is 4 chars!", Dialog.TYPE_ERROR, null, "Ok", null);
            // return focus to this component
            cmp.requestFocus(); 
            // this was added as Shai suggested
            Display.getInstance().callSerially(new Runnable() {
                public void run() {
                    Display.getInstance().editString(cmp, ((TextField) cmp).getMaxSize(), ((TextField) cmp).getConstraint(), ((TextField) cmp).getText());
                }
            });
        }
    }
});

If non-valid text entered, do not let user out of textField (either by pressing "next" on VK, or by touching outside of textfield). Simple as that.

Chen Fishbein

unread,
Mar 30, 2014, 5:14:00 AM3/30/14
to codenameone...@googlegroups.com, computer.s...@gmail.com
Hi,
I wasn't able to reproduce your issue, but I think you should let the focusLost method return and not blocking it with the dialog popup try this:

            public void focusLost(final Component cmp) {
                
Reply all
Reply to author
Forward
0 new messages