Android Question Edittext Mask

Discussion in 'Android Questions' started by Edu Chamon, Aug 9, 2019.

  1. Edu Chamon

    Edu Chamon Member Licensed User

    Good night. Please would like to show a mask while the user is typing in editext.
    for example, value in Brazil.

    1,234,567.89

    how to go showing this to the user?

    0.01
    0.12
    1.23
    12.34
    1,23.45 ........
    thank you very much
     
  2. emexes

    emexes Well-Known Member Licensed User

    In the value change event, try:
    Code:
    'remove existing decimal point, leaving string of digits
    NewString = NewString.Replace(".""")

    'remove excess leading zeroes
    Do While NewString.Length > 3
        
    If NewString.CharAt(0) <> "0" Then
            
    Exit
        
    End If

        NewString = NewString.SubString(
    1)
    Loop

    'ensure at least three digits
    Do While NewString.Length < 3
        NewString = 
    "0" & NewString
    Loop

    Dim DigitsBefore As Int = NewString.Length - 2

    'insert decimal point
    NewString = NewString.SubString2(0, DigitsBefore) & "." & NewString.SubString(DigitsBefore)
     
    Edu Chamon and BillMeyer like this.
  3. DonManfred

    DonManfred Expert Licensed User

  4. Edu Chamon

    Edu Chamon Member Licensed User

    Very nice, thank you so much, DonManfred , Emexes
     
  5. Edu Chamon

    Edu Chamon Member Licensed User

    Sorry to continue with this and the question.

    I had even looked at the links before in the forum.

    At home I test on B4a, here I am trying on B4j.

    I used as reference the field Textarea.

    In TextArea1_TextChanged event (Old As String, New As String)


    In the case of the example above or if I triggered a sub

    NewString = Old

    .
    .
    .
    .

    New = NewString


    It would be like this ?

    Thank you for understanding
     
  6. emexes

    emexes Well-Known Member Licensed User

    My previous effort was written from memory on a small-screen mobile device during a lull at a wake, so... the odds of it working first-up were low(ish).

    The posts indicated by DonManfred look good, but if you still want to roll your own simple version, then...

    Try this instead, where EditMaskCashRegister is a general routine that you can use for multiple EditTexts, and TextArea1_TextChanged is an example of its use, using the view name you indicated:
    Code:
    Sub TextArea1_TextChanged (Old As String, New As String)
      
        EditMaskCashRegister(TextArea1, Old, New)
      
    End Sub
      
    'automatically adds/moves decimal point for 0.00 format  
    Sub EditMaskCashRegister(ViewHandle As EditText, Old As String, New As String)
      
        
    'remove existing decimal point, leaving string of digits
        Dim Temp As String = New.Replace(".""")
      
        
    'remove excess leading zeroes
        Do While Temp.Length > 3
            
    If Temp.CharAt(0) = "0" Then
                Temp = Temp.SubString(
    1)
            
    Else
                
    Exit
            
    End If
        
    Loop
      
        
    'ensure at least three digits
        Do While Temp.Length < 3
            Temp = 
    "0" & Temp
        
    Loop
      
        
    Dim DigitsBefore As Int = Temp.Length - 2
      
        
    'insert decimal point
        Temp = Temp.SubString2(0, DigitsBefore) & "." & Temp.SubString(DigitsBefore)
      
        
    If Temp <> New Then
            
    Log(New & " ==> " & Temp)
          
            
    'put field back to view
            ViewHandle.Text = Temp

            
    'postion cursor at end
            ViewHandle.SelectionStart = Temp.Length
        
    End If
      
    End Sub
     
    Edu Chamon likes this.
  7. Edu Chamon

    Edu Chamon Member Licensed User

    my sincere feelings and thank you very much
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice