EditText长度超过限制后如何停止输入 [英] How to stop taking input after EditText length crosses a limit
问题描述
我知道已经问了几个问题,但是我有一些不同的查询.
I know there are a couple of questions already been asked, but I have a little different query.
我们如何限制在EditText
中输入的最大字符数?我是否这样做
How do we put a limit on the maximum number of characters to be inputted in EditText
? Whether I do this
android:maxLength="7"
或这个
((EditText)findViewById(R.id.zip)).setFilters(new InputFilter[] {
new InputFilter.LengthFilter(7)
});
我仍然可以在文本字段中输入内容,尽管它是不可见的,也没有考虑在内.如果我继续输入,则输入将在限制之后变得不可见,但是test-suggestions
会继续为更大数量的字母而构建.
I can still type into the text field, though it's not visible and not taken into account. If I keep typing, the input becomes invisible after the limit, but the test-suggestions
keep building for higher number of letters.
假设我的限制是7个,我输入了10个字母,那么我只会看到前7个字母,但是文本建议将显示10个字母的单词.此外,我需要按3次退格键才能真正开始从显示的7个字母的单词中删除字母.
Suppose my limit is 7 and I typed 10 letters, then I would only see the first 7 letters, but text-suggestions will show 10 letter words. Furthermore, I need to press backspace 3 times to actually start deleting letters from my shown 7-letter word.
所以,我的问题是:有没有办法在一定的字符长度后停止接受输入?
So, my question is: Is there a way to stop taking input after a certain character length?
PS :不用说,尽管我不希望在7个字母之后输入内容,但我允许删除或退格.
PS: Needless to say, but though I don't want input to be inputted after 7 letters, I would allow delete or backspace.
推荐答案
只需将其添加到您的EditText中即可.肯定会工作.
Just add this to your EditText. It will definitely work.
android:inputType="textFilter"
我尝试了同样的方法,并且对我有用.
I tried the same and it worked for me.
这篇关于EditText长度超过限制后如何停止输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!