最大长度

我在 EditText 字段中设置了文本的最大长度。

<EditText
android:id="@+id/courseDescriptionField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5"
android:maxLength="@integer/max_course_description_limit"
android:gravity="left|top" >
</EditText>

然而,对我来说问题是,文本停止出现在140个字符之后,但它仍然继续输入,除了文本只是不出现,但是,它确实出现在“缓冲区”(意思是建议的事情) ,如果这是你会调用它。

另外,我使用 TextWatcher 来跟踪限制。有没有什么方法可以完全限制文本的数量,这样当有140个字符时,按下退格/删除键之外的其他操作就不会发生?

116441 次浏览

Possible duplicate of Limit text length of EditText in Android

Use android:maxLength="140"

That should work. :)

Hope that helps

I had the same problem.

Here is a workaround

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

Thx to How can I turnoff suggestions in EditText?

EditText editText= ....;
InputFilter[] fa= new InputFilter[1];
fa[0] = new InputFilter.LengthFilter(8);
editText.setFilters(fa);

I had the same problem. It works perfectly fine when you add this:

android:inputType="textFilter"

to your EditText.

If you used maxLength = 6 , some times what you are entering those characters are added in top of the keyboard called suggestions. So when you deleting entered letters that time it will delete suggestions first and then actual text inside EditText. For that you need to remove the suggestions.just add

android:inputType="textNoSuggestions"`

or

android:inputType="textFilter"

It will remove those suggestions.

For me this solution works:

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

If you want to see a counter label you can use app:counterEnabled and android:maxLength, like:

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true">


<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="420" />


</android.support.design.widget.TextInputLayout>

DO NOT set app:counterMaxLength on TextInputLayout because it will conflict with android:maxLength resulting into the issue of invisible chars after the text hits the size limit.

You may try this

 EditText et = (EditText) findViewById(R.id.myeditText);
et.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(140) }); // maximum length is 140