Android EditText 最大长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12083183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android EditText Max Length
提问by Jon Wei
I set up a max length of text in an EditText field.
我在 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>
The problem for me however is, that, text STOPS appearing after 140 characters, but it still continues to type, except that text just doesn't appear, but however, it does appear in the "buffer" (meaning the suggestion thing) if that's what you would call it.
然而,对我来说的问题是,文本 STOPS 出现在 140 个字符之后,但它仍然继续输入,只是文本没有出现,但是,它确实出现在“缓冲区”中(意思是建议的东西),如果这就是你所说的。
As a side note, I am using a TextWatcher to keep track of the limit. Is there any way to completely limit the amount of text, so that when there are 140 characters, nothing happens when something other than backspace/delete is pressed?
作为旁注,我使用 TextWatcher 来跟踪限制。有什么办法可以完全限制文本的数量,这样当有 140 个字符时,按下退格/删除以外的其他东西时什么也不会发生?
回答by Ushal Naidoo
Possible duplicate of Limit text length of EditText in Android
Android 中 EditText的限制文本长度的可能重复
Use android:maxLength="140"
用 android:maxLength="140"
That should work. :)
那应该工作。:)
Hope that helps
希望有帮助
回答by mrroboaat
I had the same problem.
我有同样的问题。
Here is a workaround
这是一个解决方法
android:inputType="textNoSuggestions|textVisiblePassword"
android:maxLength="6"
回答by Samira Ekrami
EditText editText= ....;
InputFilter[] fa= new InputFilter[1];
fa[0] = new InputFilter.LengthFilter(8);
editText.setFilters(fa);
回答by Akeshwar Jha
I had the same problem. It works perfectly fine when you add this:
我有同样的问题。添加此内容时它可以正常工作:
android:inputType="textFilter"
to your EditText
.
到您的EditText
.
回答by Premkumar Manipillai
You may try this
你可以试试这个
EditText et = (EditText) findViewById(R.id.myeditText);
et.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(140) }); // maximum length is 140
回答by venkat
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
如果您使用maxLength = 6
,有时您输入的这些字符会添加到称为建议的键盘顶部。所以当你删除输入的字母时,它会先删除建议,然后删除里面的实际文本EditText
。为此,您需要删除建议。只需添加
android:inputType="textNoSuggestions"`
or
或者
android:inputType="textFilter"
It will remove those suggestions.
它将删除这些建议。
回答by Daniel Gomez Rico
If you want to see a counter label you can use app:counterEnabled
and android:maxLength
, like:
如果您想查看计数器标签,可以使用app:counterEnabled
and android:maxLength
,例如:
<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 NOTset 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.
不要设置app:counterMaxLength
,TextInputLayout
因为它会与android:maxLength
文本达到大小限制后导致不可见字符的问题发生冲突。
回答by Wojtek
For me this solution works:
对我来说,这个解决方案有效:
edittext.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);