How do I restrict my EditText input to numerical (possibly decimal and signed) input?
There's no reason to use setRawInputType()
, just use setInputType()
. However, you have to combine the class and flags with the OR operator:
edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
Try using TextView.setRawInputType() it corresponds to the android:inputType
attribute.
Use this. Works fine
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
EDIT
kotlin version
fun EditText.onlyNumbers() {
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED
keyListener = DigitsKeyListener.getInstance("0123456789")
}