Set edittext using only ",0123456789" programmatically in Android?
Solution 1
try customEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Solution 2
EditText ed;
ed.setInputType(InputType.TYPE_CLASS_NUMBER);
also docs may help
Solution 3
According to the Android documentation TYPE_NUMBER_FLAG_DECIMAL
should work http://developer.android.com/reference/android/text/InputType.html
However it doesn't, if you use only TYPE_NUMBER_FLAG_DECIMAL
the user will be able to input letters as well.
But by our luck, Haresh Chaudhary, provide the correct answer, both must be use, TYPE_CLASS_NUMBER
and also TYPE_NUMBER_FLAG_DECIMAL
. I try hes solution and It worked.
Allthough I used setInputType instead of setRawInput:
yourEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Related videos on Youtube
joonasj
Student for batchelor of science at field of information technology.
Updated on July 13, 2022Comments
-
joonasj almost 2 years
My question is this.
I can say in xml
android:digits="0123456789,"
But this time I've to it add trough java code.
customEditText.setKeyListener(DigitsKeyListener.getInstance("0123456789,"));
Doesn't work. Application gives me same result as android:InputType="number"
So is there any alternative way to say android:digits using java?
EDITED
So my question wasn't how to get
android:InputType="number"
using java.My question is that how I can get
android:digits="0123456789,"
to be used with java.At this point I don't care if the user can see characters. I just want my edittext field to accept only numbers from 0 to 9 and the decimal comma (,).
Because of the customEdittext is really a customized edittext I can't use xml atm.
-
joonasj almost 12 yearsWell I don't want to use number. Because then I can't get the decimal comma in use.
-
joonasj almost 12 yearsWell I don't want to use number. Because then I can't get the decimal comma in use.
-
joonasj almost 12 yearsWell I don't want to use number. Because then I can't get the decimal comma in use.
-
joonasj almost 12 yearsWell this time I can't, because of the code. I've to do it using java only. And I wan't to have my edittext field to accept only numbers 0-9 and decimal comma (,).
-
Andrew F almost 12 yearstry to using inputfilter developer.android.com/reference/android/text/InputFilter.html customEditText.setFilter(yourInputFilter);
-
Aswin Kumar almost 12 yearsWhat version of android are you testing on? The DigitsKeyListener.getInstance("0123456789,") is working for me on 2.3, 4.0 and 4.1
-
joonasj almost 12 yearsgalaxy s 2.3.3 and galaxy nexus 4.0.3