问题
This is my xml
<EditText
android:id="@+id/et_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions|textVisiblePassword"
android:hint="Provide comments here..."
android:gravity="top"
android:maxLength="5"
android:textSize="12sp"
android:visibility="visible"
/>
Neither is it working using this code
TextView editEntryView = new TextView(...);
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(5);
editEntryView.setFilters(filterArray);
maxLenth is not working, I dont know why, but it isnt.
I have checked other answers on the stack but they are also not working.
Please check if any of EditText attributes are conflicting or what is the problem?
EDIT: Same problem is being faced by other developers
See comments here same problem is being faced by Mansi and aat
And here in comments same problem is being faced by Vincy and Riser
EDIT: Problem solved
I was using input filter which overrides the max length in xml making it not able to work.
The reason input filter didn't worked for me was that I was using another input filter which overwrites the previous maxLength input filter.
Making it into a single input filter fixed that issue for me.
回答1:
Fairly old post but, I noticed how the XML is an actual EditText
object, while you are adding the filters to a TextView
which could handle it differently than EditText
. If you are adding an InputFilter
object manually, the xml property is overridden.
The example code on which you add InputFilter
s to the View
seems to be a TextView
object. Make sure you pull the right view and it's being cast to EditText
if you go with the manual addition of the filters--it's working for me right now.
Good luck.
回答2:
Try this, it will work for both maxlenght and input filter
month.setFilters(new InputFilter[]{new InputFilterMinMax("0", "12"), new InputFilter.LengthFilter(2)});
回答3:
If you are using InputFilter for the edittext, then maxLength will not work.
回答4:
If you already have InputFilter then maxLength will not work. You will have to create an additional InputFilter and add it:
// Filter for comma character
String blockCharacterSet = ",";
InputFilter filter = (source, start, end, dest, dstart, dend) -> {
if (source != null && blockCharacterSet.contains(("" + source))) {
return "";
}
return null;
};
// Filter for max Length
InputFilter filter1 = new InputFilter.LengthFilter(20);
// Set the filters
et_list_name.setFilters(new InputFilter[] { filter, filter1 });
来源:https://stackoverflow.com/questions/30774123/android-edittext-maxlength-not-working