問題描述
這是我的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.
您將 InputFilter
s 添加到 View
的示例代碼似乎是一個 TextView
對象.如果您手動添加過濾器,請確保您拉出正確的視圖并將其轉換為 EditText
- 它現在對我有用.
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.
祝你好運.
這篇關于android EditText maxLength 不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!