Android Question EditText with numbers

Sergey_New

Well-Known Member
Licensed User
Longtime User
How can I limit the number of digits entered in EditText?
 

Sagenut

Expert
Licensed User
Longtime User
In the Designer change the Input Type of the desired EditText to NUMBERS
numbers.jpg
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
A question about the number of digits to be entered. For example, I need to limit the input to two digits; the field shouldn't accept more.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Sorry, I misunderstood the request.
Try this
B4X:
Private Sub EditText1_TextChanged (Old As String, New As String)
    If New.Length > 2 Then
        EditText1.Text = Old
        EditText1.SelectionStart = Old.Length
    End If
End Sub
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Sagenut, emexes, thank you!
Please tell me how to transfer focus to another EditText after entering a number exceeding the limit without transferring the number itself.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
I did it this way, and it works.
B4X:
Sub Process_Globals

End Sub

Sub Globals
    Private EditDay As EditText
    Private EditMonth As EditText
    Private EditYear As EditText
    Private btnParse As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
End Sub

Sub Activity_Resume
    
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub btnParse_Click
    Log(EditDay.Text & "." & EditMonth.Text & "." & EditYear.Text)
End Sub

Private Sub EditDay_TextChanged (Old As String, New As String)
    If New.Length > 2 Then
        EditDay.Text = Old
        EditMonth.RequestFocus
    End If
End Sub

Private Sub EditMonth_TextChanged (Old As String, New As String)
    If New.Length > 2 Then
        EditMonth.Text = Old
        EditYear.RequestFocus
    End If
End Sub

Private Sub EditYear_TextChanged (Old As String, New As String)
    If New.Length > 4 Then
        EditYear.Text = Old
        EditYear.SelectionStart = 4
    End If
End Sub
I'd like the cursor to immediately move to the next element after entering the second digit in EditDay and EditMonth. Currently, this happens after entering the next digit.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Change
B4X:
If New.Length > 2 Then
to
B4X:
If New.Length > 1 Then
and remove
B4X:
EditDay.Text = Old
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
You need to change 2 to 1, and remove
B4X:
EditDay.Text = Old
I think it should work but I can't test it now.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Sorry, I misspoke. It works correctly.
If you move the cursor back to EditMonth after switching to EditDay and enter the third digit there, all three digits will be displayed.
 
Upvote 0
How can I limit the number of digits entered in EditText?
This limits input to 5 digits (or characters) total.
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="5"
/>
 

Attachments

  • Custom-Software-and-App-Developmet-Company.png
    Custom-Software-and-App-Developmet-Company.png
    295.8 KB · Views: 5
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
There are many views for that.
I tested many different InputMask-based solutions. None of them met the proposed requirements. Even AI couldn't come up with a solution due to the complex algorithm. Using three EditTexts almost solves the problem.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Thanks! I saw that. I need manual input.
 
Upvote 0
Top