Android Question how allow comma or doc (point) in number input B4XFloatTextField control.

netsistemas

Active Member
Licensed User
Longtime User
I view this thead

and i include this code:

B4X:
    Dim ime As IME
    ime.Initialize ( "IME")
    ime.SetCustomFilter(txtKilos,txtKilos.INPUT_TYPE_NUMBERS,"0123456789,.")

but
how to do this in b4x with a B4XFloatTextField control?

the final objet is allow to the user, input a decimal with comma or point in a keyboard (in spain and europe - no germany), i think the decimal is comma.

(after i replace comma with point whit this funcion

B4X:
public Sub Val(Txt As String) As Double
    If IsNumber(Txt & "") Then
        Return Txt
    Else
        Dim NewVal As String
        NewVal = Txt.Replace(",",".")
        If IsNumber(NewVal) Then
            Return NewVal
        Else
            Return 0
            
        End If
        
    End If
End Sub

)
 

Mahares

Expert
Licensed User
Longtime User
how to do this in b4x with a B4XFloatTextField control?
To set a custom filter for a B4XFloatTextFIeld:
B4X:
Dim et As EditText =B4XFloatTextField1.TextField
IME.SetCustomFilter(et,et.INPUT_TYPE_NUMBERS,"0123456789,.")
You can validate your entry to allow only one comma or only one period:
B4X:
Sub IsValid(entry As String) As Boolean  'allows only  digits, 1 comma or 1 period
    If entry.Length = 0 Then Return False
    Dim regexPattern As StringBuilder
    regexPattern.Initialize
    regexPattern.Append("\d+[.,]|\d+\d+")  'all digits but can have only 1 comma or 1 period
    Return Regex.IsMatch(regexPattern.ToString, entry)
End Sub

Sub B4XFloatTextField1_EnterPressed
    Dim str As String =B4XFloatTextField1.Text
    If Not (IsValid(str)) Then
        MsgboxAsync("not a valid entry", "Validate")
        B4XFloatTextField1.RequestFocusAndShowKeyboard
    End If
'    str=str.Replace(",",".")    'you can replace the comma with a period here if you want
'    Log("str: " & str)  
End Sub
 
Last edited:
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
SOLVED.
thanks.
whit
B4X:
TXTKILOS = B4XFloatTextField1.TextField
it was enought.


the rest of my code are ok.
 
Upvote 0
Top