Android Question Limit 3 numbers on edittext

mscientist33

Active Member
Licensed User
I have an EditText that has a default of 000. It is set to numbers only (no decimals). I need the edit text to change as the person clicks the number. Ex: It starts at 000, the user clicks 1 the number should now show 001. Then the user clicks 4 then the number becomes 014 and so on. If the number is say 614 and the user clicks the 5, the edittext should be 145, as each number is pressed it is added to the last digit and drops the first digit to always be three digits. How can this be done? I am not having any luck in the myNumberd_TextChanged event.
 

LucaMs

Expert
Licensed User
Longtime User
Activity/B4XPage level:
Private mChanging As Boolean
B4X:
Private Sub EditText1_TextChanged (Old As String, New As String)
    If Not(mChanging) Then
        mChanging = True
        Dim v As Int = "0" & New
        EditText1.Text = NumberFormat2(v, 4, 0, 0, False).SubString(1)
        EditText1.SelectionStart = EditText1.Text.Length
        mChanging = False
    End If
End Sub
 
Last edited:
Upvote 0

Spavlyuk

Active Member
Licensed User
Set input type to numbers, then:

B4X:
Private Sub myNumberd_TextChanged (Old As String, New As String)
    If myNumberd.Text.Length > 3 Then
        myNumberd.Text = New.SubString(New.Length - 3)
        myNumberd.SetSelection(3, 0)
    End If
End Sub
 
Upvote 0
Top