Android Question modifying EditText value inside EditText_TextChanged

James_Last

New Member
Licensed User
Hello

A newbie problem here I'm sure.

In the code below, the line "EEText.Text=n" will crash the app despite n being as expected. How can I modify the contents of an EditText box with the value of n, in this example a number padded with leading zeroes, as each numerical character is entered by the user please?


B4X:
Sub EEText_TextChanged (Old As String, New As String)
     Dim n As String
     If New.Length >0 Then
     n=NumberFormat2(EEText.text, 10, 0,0, False) ' pad with leading  zeroes, min str length=10
     EEText.Text=n
     Log(n)
     End If
End Sub

many thanks
 

warwound

Expert
Licensed User
Are you creating an infinite loop?

Try this:

B4X:
Sub EEText_TextChanged (Old As String, New As String)
     Dim n As String
     If New.Length >0 Then
     n=NumberFormat2(EEText.text, 10, 0,0, False) ' pad with leading  zeroes, min str length=10
     If EEText.Text<>n Then
         EEText.Text=n
     End If
     Log(n)
     End If
End Sub
 

James_Last

New Member
Licensed User
Are you creating an infinite loop?

Try this:

B4X:
Sub EEText_TextChanged (Old As String, New As String)
     Dim n As String
     If New.Length >0 Then
     n=NumberFormat2(EEText.text, 10, 0,0, False) ' pad with leading  zeroes, min str length=10
     If EEText.Text<>n Then
         EEText.Text=n
     End If
     Log(n)
     End If
End Sub
Thanks all for your support!

I agree the problem must be due to an infinite loop, thank you warwound. I implemented the above code changes and it's just fine!

I did need to add an extra line of code to put the cursor at the end of the 'updated' zero-padded string though else more char entries would add to the left e.g., "6000000005" instead of "0000000056" :)

B4X:
  If EEText.Text<>n Then ' avoids an infinite loop!
         EEText.Text=n
         EEText.SelectionStart=EEText.Text.Length
     End If
I do have one more problem that I would like to resolve if anyone can chip in please? I tried setting a limit on the max chars allowed in EEText Edit box to 10, i.e., :

B4X:
' EE Number field..
p.SetLengthFilter (EEText, 10) 'max 10-digits allowed (assume "EE" prefix ignored)
The first entered char is understandably displayed correctly as "0000000005" (for example), but then I've filled up all 10 chars to the limit and no further entries are accepted :(
 
Top