I want to prevent a user from entering a comma “,” in a EditText
, investigate what can be done with the property android:digits=””
but you have to enter the remaining characters, will there be a property to block that property in the XML?. Thanks.
This is my EditText
:
<xxxx_android_v1.com.xxxx.sap.xxxx.xxxx_v1.Utility.DTWCustomEditText
android:id="@+id/textViewDescGuardar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="8"
android:maxLines="1"
android:maxLength="50"
android:digits="@string/caracteres"
android:inputType="textPersonName" />
Block adding characters in a
EditText
:Programmatically you can do it by not allowing certain characters, you can add an InputFilter to avoid writing a certain character in a
EditText
, first create the InputFilter to validate if it contains the character you don't want and return an empty String when trying to write it:Now assign it to your
EditText
:You can even prevent other characters from being written by adding them to the variable
Adding an InputFilter disables the property
android:maxLength
, but you can add this property on the filter to re-enable it: