I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.
I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.
Here is my current code (I need to do this programmatically):
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.
If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
, the EditText accepts all input (which isn't what I want...).
I've tried combining flags (in desperation to see if it would work):
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)
That didn't work either (the EditText accepted all input as usual).
So, how do I do this?
my solution:`
Hi All I also had this problem. I use C# with Xamarin
Just a slight note that I hope someone else as well.
I have tried multiple of the methods mentioned here.
was the closest but still did not work. As Ralf mentioned, you could use
However in my C# I did not have this. instead you do it as follow:
Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!