Android Question How best to control field focus at run time

DataMiser

Member
Licensed User
I have been working on a data collection app and running into some issues with field focus. It seems that when you hit the enter key the focus goes to the next field or button or whatever may be on the view that can get focus. What I need to do is validate the entry and if no entry or invalid entry prevent the focus from going to the next field. I do however what to allow the user to tap one of the buttons on the screen even when the entry in one or more fields is not valid.

Is there a way to prevent the app from moving to the next field automatically? Is there some other preferred way to handle field navigation and validation to insure that the user enters valid data in each and every required field before moving to the next field?
 

DataMiser

Member
Licensed User
hmm... I'm not sure either of those will do what I need here. Basically what I am doing is trying to recreate an app that I wrote originally in VB.Net for a Windows Mobile device. The device has a full keyboard on it and a barcode scanner so the soft key board is not used in the app. I don't think I can use dialogs either as the end result needs to look and feel as much like the existing app as possible.

In the existing Windows app I trap the enter key where I validate the input and either move to the next logical field or inform the entry is required or entry was invalid. The app goes through the fields top to bottom but then after the last field it needs to retain some of the data and clear other pieces and move to either field 1, 3 or 5 depending on the type of data being collected. For the most part I have managed to get it to work but I am finding that at times the focus does not end up where I tell it to go. I plan to test a few things and see how they work, I'm sure there has to be a way to pull it off.

I was hoping there was some method or setting in there that could prevent the focus from changing fields on its own.
 
Upvote 0

DataMiser

Member
Licensed User
Ok so I have been doing more testing and this is what I am running into and it does not make much sense to me.
The last field on the page under some conditions needs to loop and collect x pieces of data. What I am seeing is that when enter is pressed the first time that entry is processed and the focus is returned to that field for the next entry. The next time the entry is processed but focus ends up on the button below. I tap the input field to get focus again and again I enter data, press enter and focus returns to the field for the next entry, I do it again and again it ends up of the button. I have added some info into the log to try and see why it is working part of the time and not working part of the time. In both cases the same code is executing, in both cases it is requesting focus but it seems to only be working every other time. If there was a way to detect focus on the button I could add some code there to put focus back where it needs to be but so far I do not see a way to detect focus going to a button.
 
Upvote 0

DataMiser

Member
Licensed User
I think I found a work around to get the results I need. The issue seems to be related to the enter key moving the focus and when that focus change occurs. Sometimes my request focus code was executing first and did not end up with the focus where I wanted it. I have create a status var to indicate whether or not the focus should stay put then added code to the focus change event to try and insure the focus ends up where I need it to be. I still have more testing to do but so far it appears to be working.
 
Upvote 0

DataMiser

Member
Licensed User
Here is a little piece of code I used that seems to do the trick
sample:
Private Sub txtQty_EnterPressed
    If txtQty.Text.Length>0 Then
        If IsNumber(txtQty.text) Then
            NavMode=0
            ModCommon.soundgood
        Else
            NavMode=1
            ModCommon.soundbad
            MsgboxAsync("Must be numeric", "Entry Error")
        End If      
    Else
        NavMode=1
        ModCommon.soundbad
        MsgboxAsync("Entry required", "Entry Error")             
    End If
End Sub

Private Sub txtQty_FocusChanged (HasFocus As Boolean)  
    If HasFocus=False Then
        If NavMode=0 Then
            txtSerial.requestfocus
        Else
            txtQty.requestfocus
        End If
    Else
        txtQty.selectall
    End If
End Sub
 
Upvote 0

DataMiser

Member
Licensed User
Well that method seemed like it would work at first but once I started dealing with more fields it wasn't quite right. I do think I found a method that will work in my app though, along those same lines but using a case statement in the focus change and the mode var holding the number of the field which should be getting the focus. Still more testing to do but at this point it looks like it is going to work.
 
Upvote 0
Top