简体   繁体   中英

android EditText maxLength not working

This is my 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?

EDIT: Same problem is being faced by other developers
See comments here same problem is being faced by Mansi and aat
And here in comments same problem is being faced by Vincy and Riser

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.

试试这个,它适用于 maxlenght 和输入过滤器

month.setFilters(new InputFilter[]{new InputFilterMinMax("0", "12"), new InputFilter.LengthFilter(2)});

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.

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.

Good luck.

如果您对编辑文本使用 InputFilter,则 maxLength 将不起作用。

If you already have InputFilter then maxLength will not work. You will have to create an additional InputFilter and add it:

    // 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 });

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}

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.

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.

In my case DataBinding is used, but someone changed filter right in code using ViewBinding.

binding.edit_text.filters = arrayOf<InputFilter>(object : InputFilter {
    override fun filter(
        source: CharSequence?,
        start: Int,
        end: Int,
        dest: Spanned?,
        dstart: Int,
        dend: Int
    ): CharSequence {
        return source.toString().filter { it.toString().matches(("[${LETTERS}]").toRegex()) }
    }
})

Android Studio didn't find what method referred to "@+id/edit_text" until I removed that id from the EditText .

If you are using InputFilter for the edittext, then maxLength will not work.

It's correct if you add a filter with replacing original filters.

You can use android:maxLength="5" in your xml.

And in your code just add a filter without replacing existed filters.

Kotlin:

editText.filters = arrayOf(
  *editText.filters,
  InputFilter.AllCaps(), // your filter
  // other filters
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM