Android Question control the insertion in a edit text.

anallie0

Active Member
Licensed User
Longtime User
hi
I must control the insertion in a text edit.
the values must lie between 1 and 360
I wrote this code but it crashes the app

B4X:
Sub numTxt_TextChanged (Old As String, New As String)
If New.Length > 3 Then numTxt.text = Old   
If sf.Val(New)< 1 OR  sf.Val(New) > 360 Then numTxt.Text=""
End Sub

Logs



java.lang.StackOverflowError


at android.text.SpanSet.init(SpanSet.java:46)
at android.text.TextLine.set(TextLine.java:216)
at android.text.Layout.getHorizontal(Layout.java:854)
at android.text.Layout.getHorizontal(Layout.java:833)
at android.text.Layout.getPrimaryHorizontal(Layout.java:808)
at android.text.Layout.getPrimaryHorizontal(Layout.java:798)
at android.widget.TextView.getFocusedRect(TextView.java:6329)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:120)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:94)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:65)
at android.view.ViewGroup.focusSearch(ViewGroup.java:691)
at android.view.ViewGroup.focusSearch(ViewGroup.java:693)
at android.view.ViewGroup.focusSearch(ViewGroup.java:693)
at android.view.ViewGroup.focusSearch(ViewGroup.java:693)
at android.view.ViewGroup.focusSearch(ViewGroup.java:693)
at android.view.View.focusSearch(View.java:6851)
at android.widget.TextView.onCreateInputConnection(TextView.java:6936)


at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1233)
at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:1183)
at android.widget.TextView.setText(TextView.java:4655)
at android.widget.TextView.setText(TextView.java:4572)
at android.widget.EditText.setText(EditText.java:109)
at android.widget.TextView.setText(TextView.java:4547)
at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
at anywheresoftware.b4a.objects.EditTextWrapper.setText(EditTextWrapper.java:213)
at b4a.example.main._numtxt_textchanged(main.java:654)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:161)
at anywheresoftware.b4a.objects.EditTextWrapper$1.afterTextChanged(EditTextWrapper.java:83)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8740)
at android.widget.TextView.setText(TextView.java:4744)
at android.widget.TextView.setText(TextView.java:4572)
at android.widget.EditText.setText(EditText.java:109)
at android.widget.TextView.setText(TextView.java:4547)
at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
at anywheresoftware.b4a.objects.EditTextWrapper.setText(EditTextWrapper.java:213)
at b4a.example.main._numtxt_textchanged(main.java:654)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:161)
at anywheresoftware.b4a.objects.EditTextWrapper$1.afterTextChanged(EditTextWrapper.java:83)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8740)
at android.widget.TextView.setText(TextView.java:4744)
at android.widget.TextView.setText(TextView.java:4572)
at android.widget.EditText.setText(EditText.java:109)
at android.widget.TextView.setText(TextView.java:4547)
at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
at anywheresoftware.b4a.objects.EditTextWrapper.setText(EditTextWrapper.java:213)
at b4a.example.main._numtxt_textchanged(main.java:654)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:161)
at anywheresoftware.b4a.objects.EditTextWrapper$1.afterTextChanged(EditTextWrapper.java:83)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8740)
at android.widget.TextView.setText(TextView.java:4744)
at android.widget.TextView.setText(TextView.java:4572)
at android.widget.EditText.setText(EditText.java:109)
at android.widget.TextView.setText(TextView.java:4547)
at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
at anywheresoftware.b4a.objects.EditTextWrapper.setText(EditTextWrapper.java:213)
 

Mahares

Expert
Licensed User
Longtime User
Are you looking for something like this:
B4X:
Sub numTxt_TextChanged (Old As String, New As String)
   If New.Length > 3 Then 
    numTxt.Text= Old 
    numTxt.SelectionStart =numTxt.Text.Length 
   End If
   If IsNumber(New) AND IsNumber(Old) Then
      If New < 1 OR  New > 360 Then numTxt.Text=""
   End If
End Sub
 
Upvote 0

anallie0

Active Member
Licensed User
Longtime User
thank you Mahares
I deleted the length control because the control value obviates any need the previous

I also eliminated the
B4X:
AND IsNumber(Old)
because when you enter the first number, the value Old is null and never satisfies the function.

B4X:
Sub numTxt_TextChanged (Old As String, New As String)
      If IsNumber(New)  Then
      If New < 1 OR  New > 360 Then numTxt.Text=""
   End If
End Sub

now works
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I deleted the length control because the control value obviates any need the previous
Tha is great Anallie0. The only reason I left the length control is because a user can conceivably enter a number that starts with a 0 like this: 0657 which is still numeric. It is unlikely, but it can happen.
 
Upvote 0
Top