question

chbernard avatar image
0 Votes"
chbernard asked RobCaplan edited

xamarin android edit text with decimal value

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

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT edited

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.


[1]: https://imgur.com/a/Fc3RNwS


image.png (178.4 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello,
thanks a lot for this fast solution

have a nice day

0 Votes 0 ·

You are welcome, happy coding.

0 Votes 0 ·