How to stop taking input after EditText length crosses a limit

跟風遠走 提交于 2019-12-05 23:43:50

问题


I know there are a couple of questions already been asked, but I have a little different query.

How do we put a limit on the maximum number of characters to be inputted in EditText? Whether I do this

android:maxLength="7"

or this

((EditText)findViewById(R.id.zip)).setFilters(new InputFilter[] {
    new InputFilter.LengthFilter(7)
});

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.

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: Needless to say, but though I don't want input to be inputted after 7 letters, I would allow delete or backspace.


回答1:


Just add this to your EditText. It will definitely work.

 android:inputType="textFilter"

I tried the same and it worked for me.




回答2:


try this,

android:inputType="textNoSuggestions|textVisiblePassword"
android:maxLength="7"

OR

edittext.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

OR

android:inputType="textFilter"



回答3:


This finally works for me:

edittext.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);



回答4:


As @Madhur sugested, in your EditText xml add next line:

Documentation

android:maxLength="7"


来源:https://stackoverflow.com/questions/35089117/how-to-stop-taking-input-after-edittext-length-crosses-a-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!