java android EditText maxLength 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30774123/
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 maxLength not working
提问by Salmaan
This is my xml
这是我的 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"
/>
Neither is it working using this code
使用此代码也不起作用
TextView editEntryView = new TextView(...);
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(5);
editEntryView.setFilters(filterArray);
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?
maxLenth 不起作用,我不知道为什么,但它不是。
我已经检查了堆栈上的其他答案,但它们也不起作用。
请检查是否有任何 EditText 属性冲突或有什么问题?
EDIT: Same problem is being faced by other developers
See comments heresame problem is being faced by Mansi and aat
And herein comments same problem is being faced by Vincy and Riser
编辑:同样的问题所面临的其他开发人员
看评论这里同样的问题所面临曼西和AAT
而这里在同样的问题所面临的泳儿和提升评论
EDIT: 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 中的最大长度,使其无法工作。
输入过滤器对我不起作用的原因是我使用了另一个输入过滤器,它覆盖了以前的 maxLength 输入过滤器。
把它变成一个单一的输入过滤器为我解决了这个问题。
采纳答案by Armando
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.
相当老的帖子,但是,我注意到 XML 是一个实际的EditText
对象,而您将过滤器添加到 a TextView
,它可以与EditText
. 如果您InputFilter
手动添加对象,则 xml 属性将被覆盖。
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.
将InputFilter
s添加到 的示例代码View
似乎是一个TextView
对象。EditText
如果您手动添加过滤器,请确保您拉出正确的视图并且它正在被投射- 它现在对我有用。
Good luck.
祝你好运。
回答by Surya V
Try this, it will work for both maxlenght and input filter
试试这个,它适用于 maxlenght 和输入过滤器
month.setFilters(new InputFilter[]{new InputFilterMinMax("0", "12"), new InputFilter.LengthFilter(2)});
回答by Tripaty Sahu
If you are using InputFilter for the edittext, then maxLength will not work.
如果您对编辑文本使用 InputFilter,则 maxLength 将不起作用。
回答by live-love
If you already have InputFilter then maxLength will not work. You will have to create an additional InputFilter and add it:
如果您已经有 InputFilter,则 maxLength 将不起作用。您必须创建一个额外的 InputFilter 并添加它:
// Filter for comma character
String blockCharacterSet = ",";
InputFilter filter = (source, start, end, dest, dstart, dend) -> {
if (source != null && blockCharacterSet.contains(("" + source))) {
return "";
}
return null;
};
// Filter for max Length
InputFilter filter1 = new InputFilter.LengthFilter(20);
// Set the filters
et_list_name.setFilters(new InputFilter[] { filter, filter1 });
回答by Prajwal W
To add character restriction as well as input limit.
添加字符限制以及输入限制。
private String BlockCharacterSet_Name = "\@$#!=><&^*+\"\'";
mFirstName.setFilters(new InputFilter[] {new InputFilter.LengthFilter(25),inputFilter_Name});
private InputFilter inputFilter_Name = new InputFilter() { //filter input for name fields
@Override
public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
if (charSequence != null && BlockCharacterSet_Name.contains(("" + charSequence))) {
return "";
}
return null;
}
};
{new InputFilter.LengthFilter(25),inputFilter_Name}
{new InputFilter.LengthFilter(25),inputFilter_Name}
allows you to set both max length limitation as well as your special characters restrictions both in the same InputFilter and therefore you dont need to create separate Inputfilter for the same.
允许您在同一个 InputFilter 中设置最大长度限制以及特殊字符限制,因此您不需要为相同的 InputFilter 创建单独的 Inputfilter。
回答by Amar Jain
Try this:
试试这个:
val existingFilters: Array<InputFilter> = editText.filters
editText.filters = existingFilters.apply { plus(filter) }
You need to get the list of existing filters like maxLength applied through XML and then add more filters to it. In your case, the maxLength property of the editText was overridden by the custom InputFilter you were trying to apply.
您需要获取现有过滤器的列表,例如通过 XML 应用的 maxLength,然后向其中添加更多过滤器。在您的情况下,您尝试应用的自定义 InputFilter 覆盖了 editText 的 maxLength 属性。