Block the comma key "," in Android Studio XML

2

I want to prevent a user from entering a comma "," in a EditText , investigate what can be done with the property of android:digits=”” but you have to enter the remaining characters, there will 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" />
    
asked by Javier fr 27.02.2018 в 21:09
source

1 answer

3

Block adding characters in EditText :

Programatically you can do it by not allowing certain characters, you can add a InputFilter to avoid typing a certain character in EditText , first create the InputFilter to validate if it contains the character you do not want and return an empty String when trying to write it:

private String charactersForbiden = ","; //*Caracter o caracteres no permitidos.

private InputFilter inputfilter = new InputFilter() {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

        if (source != null && charactersForbiden .contains(("" + source))) {
            return "";
        }
        return null;
    }
};

Now assign it to your EditText :

EditText editText = (EditText) findViewById(R.id.myEditText);
editText.setFilters(new InputFilter[] { inputfilter });

You can even avoid writing other characters by adding them to the variable

  private String charactersForbiden = ",%$ñ[*]";

When you add a InputFilter the android:maxLength property is disabled, but you can add this property in the filter to re-enable it:

myEditText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(50), inputfilter });
    
answered by 27.02.2018 / 21:20
source