Hi All, this is my first post here an also my first android app ( first overall program ), a simple calculator.
I want to limit the user to type no more than one dot, I do not know how to do it. If you look at the image I uploaded you can see that I can type as many as I want.
this is the simple code I have for that button
B4X:
Sub Button11_Click
Vibrate.Vibrate(30)
EditText1.Text = EditText1.Text & "."
End Sub
Dim EditText1 As EditText
EditText1.Initialize(Null)
EditText1.Text = "012345.6789"
If EditText1.Text.Contains(".") Then ToastMessageShow("I found ....... so do not add it again :-)", False)
Dim EditText1 As EditText
EditText1.Initialize(Null)
EditText1.Text = "012345.6789"
If EditText1.Text.Contains(".") Then ToastMessageShow("I found ....... so do not add it again :-)", False)
Dim EditText1 As EditText
EditText1.Initialize(Null)
EditText1.Text = "012345.6789"
If EditText1.Text.Contains(".") Then ToastMessageShow("I found ....... so do not add it again :-)", False)
looking at it again. using this code, as soon as I press the "." button it will not let me type anything else. not even Clear button
B4X:
Sub Button11_Click
Vibrate.Vibrate(30)
EditText1.Text = EditText1.Text & "."
EditText1.Initialize(Null)
EditText1.Text = "012345.6789"
If EditText1.Text.Contains(".") Then
ToastMessageShow("blabla", False)
End If
End Sub
You could always check for a dot press before hand, when the dot is pressed it sets a flag(Boolean) to True so that you know that a dot has been used, thus it can't be used again until the C button is presses.
Or you can always use a regular expression RegEx. use [.] as the pattern. The dot is a special character in a Regular Expressions, so you would have to escape the character first...
Sub EditText1_TextChanged (Old As String, New As String)
If Old.Contains(".") Then
If Asc(New.SubString(New.Length-1)) = Asc(".") Then
EditText1.Text = Old
EditText1.SelectionStart = Old.Length
End If
End If
End Sub
Sub EditText1_TextChanged (Old As String, New As String)
If Old.Contains(".") Then
If Asc(New.SubString(New.Length-1)) = Asc(".") Then
EditText1.Text = Old
EditText1.SelectionStart = Old.Length
End If
End If
End Sub
using this code, for example if I type 2.2 then it works it wil not let me type another ".".
but if I type 2. and then I press "." again it will crash the app, see attached file
Well I think I solved the problem , I used this code and it worked
B4X:
Sub Button11_Click
Vibrate.Vibrate(30)
If EditText1.Text.Contains(".") Then
EditText1.Text = EditText1.Text
Else
EditText1.Text = EditText1.Text & "."
End If
End Sub
Be careful to use the point as a decimal: English syntax provides the point as a decimal and a comma to separate the thousands elsewhere (in Italy, for example) vice versa
This example from Klaus' guide, I had used before.
B4X:
AddPeriod
If cboRName.SelectedItem.Length >0 Then
If SelectQTY_or_LotNo= 1 AND LayOutDefault= True Then
If txtQty.text.Length > 0 Then
If txtQty.text = Round(txtQty.text) Then 'checks if the user already pressed period or 0 is the first char
writeNumber(".")
End If
Else
writeNumber("0") 'adds 0 if it is the first char
writeNumber (".")
End If
End If
End If
End Sub
and filter use alike this
B4X:
Dim IME As IME
IME.Initialize("IME")
IME2SetCustomFilter(send,send.INPUT_TYPE_NUMBERS, "0123456789.")
this will allow users to type a single Decimal point then allow ONLY 0-9 entries.. (Disables the decimal key)
in globals place
B4X:
Dim FilterFlag As Boolean = False
then ...
B4X:
Sub EditText1_TextChanged (Old As String, New As String)
If FilterFlag = False Then
If New.Contains(".") Then
FilterFlag = True
FilterEntrys
End If
End If
End Sub
Sub FilterEntrys
Dim im As IME
Dim sb As StringBuilder
sb.Initialize
For i = 48 To 57
sb.Append(Chr(i)) '0 - 9
Next
im.SetCustomFilter(EditText1, Bit.OR(EditText1.INPUT_TYPE_DECIMAL_NUMBERS,524288) , sb.ToString) '524288 - no suggestions
End Sub
this will allow users to type a single Decimal point then allow ONLY 0-9 entries.. (Disables the decimal key)
in globals place
B4X:
Dim FilterFlag As Boolean = False
then ...
B4X:
Sub EditText1_TextChanged (Old As String, New As String)
If FilterFlag = False Then
If New.Contains(".") Then
FilterFlag = True
FilterEntrys
End If
End If
End Sub
Sub FilterEntrys
Dim im As IME
Dim sb As StringBuilder
sb.Initialize
For i = 48 To 57
sb.Append(Chr(i)) '0 - 9
Next
im.SetCustomFilter(EditText1, Bit.OR(EditText1.INPUT_TYPE_DECIMAL_NUMBERS,524288) , sb.ToString) '524288 - no suggestions
End Sub