iOS Question Tabbing between textfields

simplypats

Member
Licensed User
Longtime User
I have a series of textfields on a layout which I wish the user to be able to 'TAB' between each.

I have set the textfield.ReturnKey= textfield.RETURN_NEXT on each of the textfields so that NEXT appears in the keyboard.

Pressing Next on the keyboard doesn't seem to set the cursor automatically in the next field so I have added a textfield_EndEdit event to each textfield to set the focus in the next text field.

This seems to work initially however it then repeatedly fires the EndEdit events of the text fields in sequence sending the focus around the fields automatically in a loop before eventually randomly stopping on one of the fields.

I'm fairly new to iOS development and wonder what is the best way to implement tabbing between textfields? Would be grateful for any help.
 

Yvon Steinthal

Active Member
Licensed User
What i do is keep a "tag" on each individual textfields that incremented from 1 to the number of textfields and keep a global variable "index" to have a cursor of sorts. I then loop through my textfield object to find the one that has the right "tag" (or index + 1). I just RequestFocus on the one found.

I am very unsure if its the best way to do this but it works well.

B4X:
For each v as view in pnl.getallviewsrecursive
if(v is TextField)Then
if(v.tag = index +1 )Then
Dim txtf as Textfield = v
txtf.requestfocus
index = index +1
end if
end if
Next
 
Upvote 0

simplypats

Member
Licensed User
Longtime User
Many thanks Yvon, I will give this a go.

Can I ask on what event do you call this routine? Each textfield_EndEdit?

Thanks again for you help.
 
Upvote 0

Graeme Mitchell

Member
Licensed User
I tried this on a button click routine. If finds the 4 text fields but it never finds a match between v.tag and index +1. When i break on that line i do see it match but it never drops down on the the Dim txtf as TextField = v line
 
Upvote 0

Yvon Steinthal

Active Member
Licensed User
I tried this on a button click routine. If finds the 4 text fields but it never finds a match between v.tag and index +1. When i break on that line i do see it match but it never drops down on the the Dim txtf as TextField = v line

Make sure the tag is of the same type "string" vs "int" might cause problem?
 
Upvote 0

Graeme Mitchell

Member
Licensed User
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'Public variables can be accessed from all modules.
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page

   Private button1 As Button
   Private TextField1 As TextField
   Private TextField2 As TextField
   Private TextField3 As TextField
   Private pnl As Panel
   Dim index As Int
End Sub

Private Sub Application_Start (Nav As NavigationController)
   'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
   NavControl = Nav
   Page1.Initialize("TextFields")
   Page1.Title = "Dashboard"
   Page1.RootPanel.LoadLayout("TextFields")
   NavControl.ShowPage(Page1)
End Sub

Sub Button1_Click
   Dim counter As Int
   counter = index +1
   For Each v As View In pnl.getallviewsrecursive
       If(v Is TextField)Then
           If(v.tag = counter )Then
               Dim txtf As TextField = v
               txtf.requestfocus
               index = index +1
           End If
       End If
   Next
   
End Sub
 
Upvote 0

Yvon Steinthal

Active Member
Licensed User
Maybe its my misunderstanding but you're not setting index to 0 anywhere in the code?
Or whatever your first textfield's tag is...
 
Upvote 0

Yvon Steinthal

Active Member
Licensed User
There's something wrong with the condition, at some level the two are not equal (v.tag and index) else it would fall in the IF statement...
 
Upvote 0

Graeme Mitchell

Member
Licensed User
upload_2019-10-4_14-37-38.png
 
Upvote 0

Yvon Steinthal

Active Member
Licensed User
I still suspect that the Textfield tag is read as a "STRING" type and the other tag is an "INT" type. Here's a simple test to make sure im wrong:

B4X:
For Each v As View In pnl.getallviewsrecursive
       If(v Is TextField)Then
           Dim txtTag as string = v.tag
           Dim strTag as String = counter 'to make sure we compare two strings
           If(txtTag = strTag )Then
               Dim txtf As TextField = v
               txtf.requestfocus
               index = index +1
           End If
       End If
   Next
 
Upvote 0
Top