Android Question How to advance to next TEdittext, by code?

vecino

Well-Known Member
Licensed User
Longtime User
Hi, how can I simulate pressing the "next" key to advance between different TEdittext.
I want to do it by code.
Thank you very much.
ummm.png
 

jkhazraji

Active Member
Licensed User
Longtime User
Hi, how can I simulate pressing the "next" key to advance between different TEdittext.
I want to do it by code.
Thank you very much.
View attachment 154712
Try this:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
Sub Process_Globals
    ' These global variables will be declared once when the application starts.
    ' Public variables can be accessed from all modules.
End Sub

Sub Globals
    ' These global variables will be redeclared each time the activity is created.
    Private EditText1 As EditText
    Private EditText2 As EditText
    Private EditText3 As EditText
    Private EditText4 As EditText
    Private EditText5 As EditText
    Private EditText6 As EditText
    Private EditText7 As EditText
    Private EditText8 As EditText
    
    Private Button1 As Button
    ' Variable to keep track of the current focus
    Private currentFocusIndex As Int
    
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout") ' Load the layout with EditText fields and Button
    
    ' Initialize the current focus index
    currentFocusIndex = 1
    
    ' Set initial focus to EditText1
    EditText1.RequestFocus
End Sub

Sub Button1_Click
    ' Change focus to the next EditText
    ChangeFocus
End Sub

Sub ChangeFocus
    Select currentFocusIndex
        Case 1
            EditText2.RequestFocus
            currentFocusIndex = 2
        Case 2
            EditText3.RequestFocus
            currentFocusIndex = 3
        Case 3
            EditText4.RequestFocus
            currentFocusIndex = 4
        Case 4
            EditText5.RequestFocus
            currentFocusIndex = 5
        Case 5
            EditText6.RequestFocus
            currentFocusIndex = 6
        Case 6
            EditText7.RequestFocus
            currentFocusIndex = 7
        Case 7
            EditText8.RequestFocus
            currentFocusIndex = 8
        Case 8
            EditText1.RequestFocus
            currentFocusIndex = 1
        
    End Select
End Sub
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
If you want to show the keyboard while advancing the focus try the following:
B4X:
Sub ChangeFocus
    Select currentFocusIndex
        Case 1
           ime.ShowKeyboard(EditText2)
            currentFocusIndex = 2
        Case 2
            ime.ShowKeyboard(EditText3)
            currentFocusIndex = 3
        Case 3
            ime.ShowKeyboard(EditText4)
            currentFocusIndex = 4
        Case 4
            ime.ShowKeyboard(EditText5)
            currentFocusIndex = 5
        Case 5
            ime.ShowKeyboard(EditText6)
            currentFocusIndex = 6
        Case 6
            ime.ShowKeyboard(EditText7)
            currentFocusIndex = 7
        Case 7
           ime.ShowKeyboard(EditText8)
            currentFocusIndex = 8
        Case 8
            ime.ShowKeyboard(EditText1)
            currentFocusIndex = 1
        
    End Select
End Sub
 
Upvote 1

Sagenut

Expert
Licensed User
Longtime User
The solution proposed by @jkhazraji is correct and work, but in the case of future expansion it will be more hard to maintain.
When you see that you need to write many times the same code you must get the doubt that there can be a better way to do it.
As @Erel teach us HERE.
Here is my proposal
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private edt1 As EditText
    Private edt2 As EditText
    Private edt3 As EditText
    Private edt4 As EditText
    Private edt5 As EditText
    Private edt6 As EditText
    Private edt7 As EditText
    Private edt8 As EditText
    Private edtArray() As EditText    'Prepare an Array to hold all the EditTexts
    Private count As Int = 0        'Variable to always move to next EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    edtArray = Array As EditText (edt1, edt2, edt3, edt4, edt5, edt6, edt7, edt8)    'Filling the Array with the Editexts that you want
End Sub

Sub Button1_Click
    ChangeFocus        'Call the Sub to move focus
End Sub

Private Sub ChangeFocus
    Dim NextEdt As EditText = edtArray(count Mod edtArray.Length)    'Make a reference EditText and assign the next Edittext from the Array that will receive the focus
    NextEdt.RequestFocus        'Request Focus
    count = count + 1            'Increase to have the next Edittext when calling the sub again
End Sub
In the need to add more EditTexts (or remove some EditTexts) it will be enough to add/remove them in the Array here
B4X:
edtArray = Array As EditText (edt1, edt2, edt3, edt4, edt5, edt6, edt7, edt8)
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
The solution proposed by @jkhazraji is correct and work, but in the case of future expansion it will be more hard to maintain.
When you see that you need to write many times the same code you must get the doubt that there can be a better way to do it.
As @Erel teach us HERE.
Here is my proposal
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private edt1 As EditText
    Private edt2 As EditText
    Private edt3 As EditText
    Private edt4 As EditText
    Private edt5 As EditText
    Private edt6 As EditText
    Private edt7 As EditText
    Private edt8 As EditText
    Private edtArray() As EditText    'Prepare an Array to hold all the EditTexts
    Private count As Int = 0        'Variable to always move to next EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    edtArray = Array As EditText (edt1, edt2, edt3, edt4, edt5, edt6, edt7, edt8)    'Filling the Array with the Editexts that you want
End Sub

Sub Button1_Click
    ChangeFocus        'Call the Sub to move focus
End Sub

Private Sub ChangeFocus
    Dim NextEdt As EditText = edtArray(count Mod edtArray.Length)    'Make a reference EditText and assign the next Edittext from the Array that will receive the focus
    NextEdt.RequestFocus        'Request Focus
    count = count + 1            'Increase to have the next Edittext when calling the sub again
End Sub
In the need to add more EditTexts (or remove some EditTexts) it will be enough to add/remove them in the Array here
B4X:
edtArray = Array As EditText (edt1, edt2, edt3, edt4, edt5, edt6, edt7, edt8)
Thanks for improving the code , but I thought it could be improved and refined further:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
Sub Process_Globals
    ' These global variables will be declared once when the application starts.
    ' Public variables can be accessed from all modules.
End Sub

Sub Globals
    ' These global variables will be redeclared each time the activity is created.
    Private EditText1 As EditText
    Private EditText2 As EditText
    Private EditText3 As EditText
    Private EditText4 As EditText
    Private EditText5 As EditText
    Private EditText6 As EditText
    Private EditText7 As EditText
    Private EditText8 As EditText
    Private Button1 As Button
    ' Variable to keep track of the current focus
    Private edtList As List             'Prepare a List to hold all the EditTexts
    Private count As Int = 0             'Variable to always move to next EditText
 
End Sub

Sub Activity_Create(FirstTime As Boolean)
    edtList.Initialize
    Activity.LoadLayout("Layout") ' Load the layout with EditText fields and Button
    For Each edt As View In Activity.GetAllViewsRecursive 'Get All Edittexts by type
        If edt Is EditText Then
             edtList.Add(edt)    'Add them to the list
        End If
    Next
    'Set initial focus to the first EditText View
    edtList.Get(0).As(EditText).RequestFocus
 
End Sub

Sub Button1_Click
    ' Change focus to the next EditText
    ChangeFocus
End Sub

Private Sub ChangeFocus
     count = count + 1            'Increase to have the next Edittext when calling the sub again
    Dim NextEdt As EditText = edtList.Get(count Mod edtList.Size).As(EditText)
    NextEdt.RequestFocus        'Request Focus
  
End Sub
 
Last edited:
Upvote 0
Top