xamarin android edit text with decimal value

cristopheB 551 Reputation points
2021-05-19T12:37:00.67+00:00

Hello,
i need to add edit text on one app.
Actually only edit text we have we can just enter some string ...

for the next release, users now can be add decimal value (13,015) ..

I would like to know which is better way to do that ?
Is it better to add two edit text ? one for the part -> 13 and a second edit text box for the 0,15
the problem I see is when user need to enter the coma some users make some errors ...

Or any idea how is the better way for to do that ?

thanks for your suggestion

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2021-05-20T06:38:02.82+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    users now can be add decimal value (13,015) .. tI would like to know which is better way to do that ?
    Is it better to add two edit text ? one for the part -> 13 and a second edit text box for the 0,15
    the problem I see is when user need to enter the coma some users make some errors ...

    Do you want to achieve the result like this screenshot?

    98088-image.png

    If so, you can achieve it by AddTextChangedListener event.

       <EditText  
               android:layout_width="match_parent"  
               android:layout_height="wrap_content"  
               android:id="@+id/editText1"  
               android:inputType="numberDecimal"/>  
    

    I write a StringUtils to handle number. And use this StringUtils in AfterTextChanged method.

       editText1 = FindViewById<EditText>(Resource.Id.editText1);  
        editText1.AddTextChangedListener(new MyTextChangedListener(editText1));  
         
         
         public static class StringUtils  
           {  
               public static string touzi_ed_values22 = "";  
         
               /**   
                * Add a comma to the thousandth of a numeric string  
                * @param str   
                * @param edtext  
                * @return sb.toString()  
                */  
               public static string addComma(string str, EditText edtext)  
               {  
         
                   touzi_ed_values22 = edtext.Text.Trim().Replace(",", "");  
         
                   bool neg = false;  
                   if (str.StartsWith("-"))  
                   {  //Handling negative numbers   
                       str = str.Substring(1);  
                       neg = true;  
                   }  
                   string tail = null;  
                   if (str.IndexOf('.') != -1)  
                   { //Handle decimal point    
                       tail = str.Substring(str.IndexOf('.'));  
                       str = str.Substring(0, str.IndexOf('.'));  
                   }  
                   StringBuilder sb = new StringBuilder(str);  
                   sb.Reverse();  
                   for (int i = 3; i < sb.Length(); i += 4)  
                   {  
                       sb.Insert(i, ',');  
                   }  
                   sb.Reverse();  
                   if (neg)  
                   {  
                       sb.Insert(0, '-');  
                   }  
                   if (tail != null)  
                   {  
                       sb.Append(tail);  
                   }  
                   return sb.ToString();  
               }  
         
         
           }  
           internal class MyTextChangedListener : Java.Lang.Object, ITextWatcher  
           {  
               public MyTextChangedListener(EditText editText1)  
               {  
                   EditText1 = editText1;  
               }  
         
               public EditText EditText1 { get; }  
         
               public void AfterTextChanged(IEditable s)  
               {  
                   // throw new NotImplementedException();  
         
                   if (!StringUtils.touzi_ed_values22.Equals(EditText1.Text.ToString().Trim().Replace(",", "")))  
                   {  
                       EditText1.Text=StringUtils.addComma(EditText1.Text.ToString().Trim().Replace(",", ""), EditText1);  
                       EditText1.SetSelection(StringUtils.addComma(EditText1.Text.ToString().Trim().Replace(",", ""), EditText1).Length);  
                   }  
         
         
               }  
         
               public void BeforeTextChanged(ICharSequence s, int start, int count, int after)  
               {  
                 //  throw new NotImplementedException();  
               }  
         
               public void OnTextChanged(ICharSequence s, int start, int before, int count)  
               {  
                  // throw new NotImplementedException();  
               }  
           }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.