EditText maxLength property not applied properly when setFilters is used

后端 未结 4 1878
暗喜
暗喜 2021-01-03 05:40

When I use setFilter method on an EditText to handle special characters, maxLength property is not working as expected. My code is be

4条回答
  •  隐瞒了意图╮
    2021-01-03 06:16

    This is because the maxLength property sets an InputFilter on your EditText. By calling EditText.setFilters(new InputFilter[] {}) you are overriding all existing InputFilters including the one used by maxLength.

    To fix this, copy the array returned by EditText.getFilters() and add your own to it:

    InputFilter[] editFilters = edit.getFilters();
    InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
    System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
    newFilters[editFilters.length] = ;
    edit.setFilters(newFilters);
    

提交回复
热议问题