android EditText maxLength 不起作用

这是我的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"
/>

使用此代码也不行

TextView editEntryView = new TextView(...);
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(5);
editEntryView.setFilters(filterArray);

maxLenth 不工作,我不知道为什么,但它不工作.
我已经检查了堆栈上的其他答案,但它们也不起作用.
请检查是否有任何 EditText 属性冲突或有什么问题?

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?

其他开发人员也面临同样的问题
查看评论这里 Mansi 和 aat 面临同样的问题
而 这里 在评论中,Vincy 和 Riser 也面临同样的问题

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

问题已解决
我正在使用输入过滤器,它覆盖了 xml 中的最大长度,使其无法工作.
输入过滤器对我不起作用的原因是我使用了另一个输入过滤器,它覆盖了以前的 maxLength 输入过滤器.
把它变成一个单一的输入过滤器为我解决了这个问题.

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.

推荐答案

相当老的帖子但是,我注意到 XML 是一个实际的 EditText 对象,而您正在将过滤器添加到 TextView 处理它的方式与 EditText 不同.如果您要手动添加 InputFilter 对象,则会覆盖 xml 属性.

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.

您将 InputFilters 添加到 View 的示例代码似乎是一个 TextView 对象.如果您手动添加过滤器,请确保您拉出正确的视图并将其转换为 EditText - 它现在对我有用.

The example code on which you add InputFilters 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.

祝你好运.

相关文章