Android Question Masked EditText: set cursor position

peacemaker

Expert
Licensed User
Longtime User
HI, All
MaskedEditTest lib v.1.52.

How to set the cursor position into the first digit position at focus received, if
B4X:
txtPhone.Format = "+7 (###) ###-##-##"
?

For correct typing start of the whole tel number.

Setting txtPhone.SelectionStart = 4 is changing the position (at this moment txtPhone.CompactText = "+7 () --"), but only while "Sub txtPhone_FocusChanged" is running, after it the position is again jumping to the tapped position.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
SOLVED:
B4X:
Private txtPhone As MaskedEditText
....

Sub txtPhone_FocusChanged(HasFocus As Boolean)
    If HasFocus Then
        If txtPhone.CompactText.Length < 9 Then   'while  txtPhone.CompactText = "+7 () --"
            CallSubDelayed3(Me, "Set_Cursor", txtPhone, 4)   'setting with a delay is OK. Direct txtPhone.SelectionStart = 4 works wrong.
        End If
    End If
End Sub

Sub Set_Cursor(v As MaskedEditText, i As Int)
    v.SelectionStart = i
End Sub

Sub txtPhone_Filter(NewText As String, Position As Int, Replace As Boolean) As String
    If txtPhone.CompactText.Length < 9 And Position > 4 Then  'if empty edittext
        txtPhone.Text = NewText  'start typing from the first digit for sure
        Return ""   'not to place the NewText into the tapped position
    End If
    Return NewText   'type new digit where the user needs to
End Sub
 
Last edited:
Upvote 0
Top