What's the best way to limit text length of EditText in Android

后端 未结 22 1906
囚心锁ツ
囚心锁ツ 2020-11-22 13:33

What\'s the best way to limit the text length of an EditText in Android?

Is there a way to do this via xml?

相关标签:
22条回答
  • 2020-11-22 14:27

    This works fine...

    android:maxLength="10"

    this will accept only 10 characters.

    0 讨论(0)
  • 2020-11-22 14:28

    it simple way in xml:

    android:maxLength="@{length}"
    

    for setting it programmatically you can use the following function

    public static void setMaxLengthOfEditText(EditText editText, int length) {
        InputFilter[] filters = editText.getFilters();
        List arrayList = new ArrayList();
        int i2 = 0;
        if (filters != null && filters.length > 0) {
            int filtersSize = filters.length;
            int i3 = 0;
            while (i2 < filtersSize) {
                Object obj = filters[i2];
                if (obj instanceof LengthFilter) {
                    arrayList.add(new LengthFilter(length));
                    i3 = 1;
                } else {
                    arrayList.add(obj);
                }
                i2++;
            }
            i2 = i3;
        }
        if (i2 == 0) {
            arrayList.add(new LengthFilter(length));
        }
        if (!arrayList.isEmpty()) {
            editText.setFilters((InputFilter[]) arrayList.toArray(new InputFilter[arrayList.size()]));
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:29

    This is a custom EditText Class that allow Length filter to live along with other filters. Thanks to Tim Gallagher's Answer (below)

    import android.content.Context;
    import android.text.InputFilter;
    import android.util.AttributeSet;
    import android.widget.EditText;
    
    
    public class EditTextMultiFiltering extends EditText{
    
        public EditTextMultiFiltering(Context context) {
            super(context);
        }
    
        public EditTextMultiFiltering(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public EditTextMultiFiltering(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        public void setMaxLength(int length) {
            InputFilter curFilters[];
            InputFilter.LengthFilter lengthFilter;
            int idx;
    
            lengthFilter = new InputFilter.LengthFilter(length);
    
            curFilters = this.getFilters();
            if (curFilters != null) {
                for (idx = 0; idx < curFilters.length; idx++) {
                    if (curFilters[idx] instanceof InputFilter.LengthFilter) {
                        curFilters[idx] = lengthFilter;
                        return;
                    }
                }
    
                // since the length filter was not part of the list, but
                // there are filters, then add the length filter
                InputFilter newFilters[] = new InputFilter[curFilters.length + 1];
                System.arraycopy(curFilters, 0, newFilters, 0, curFilters.length);
                newFilters[curFilters.length] = lengthFilter;
                this.setFilters(newFilters);
            } else {
                this.setFilters(new InputFilter[] { lengthFilter });
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:33

    Due to goto10's observation, I put together the following code to protected against loosing other filters with setting the max length:

    /**
     * This sets the maximum length in characters of an EditText view. Since the
     * max length must be done with a filter, this method gets the current
     * filters. If there is already a length filter in the view, it will replace
     * it, otherwise, it will add the max length filter preserving the other
     * 
     * @param view
     * @param length
     */
    public static void setMaxLength(EditText view, int length) {
        InputFilter curFilters[];
        InputFilter.LengthFilter lengthFilter;
        int idx;
    
        lengthFilter = new InputFilter.LengthFilter(length);
    
        curFilters = view.getFilters();
        if (curFilters != null) {
            for (idx = 0; idx < curFilters.length; idx++) {
                if (curFilters[idx] instanceof InputFilter.LengthFilter) {
                    curFilters[idx] = lengthFilter;
                    return;
                }
            }
    
            // since the length filter was not part of the list, but
            // there are filters, then add the length filter
            InputFilter newFilters[] = new InputFilter[curFilters.length + 1];
            System.arraycopy(curFilters, 0, newFilters, 0, curFilters.length);
            newFilters[curFilters.length] = lengthFilter;
            view.setFilters(newFilters);
        } else {
            view.setFilters(new InputFilter[] { lengthFilter });
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:35

    From material.io, you can use TextInputEditText combined with TextInputLayout:

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:counterEnabled="true"
        app:counterMaxLength="1000"
        app:passwordToggleEnabled="false">
    
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:hint="@string/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLength="1000"
            android:gravity="top|start"
            android:inputType="textMultiLine|textNoSuggestions"/>
    
    </com.google.android.material.textfield.TextInputLayout>
    

    You can configure a password EditText with drawable:

    Or you can limit text length with/without a counter:

    Dependency:

    implementation 'com.google.android.material:material:1.1.0-alpha02'
    
    0 讨论(0)
  • 2020-11-22 14:36

    Documentation

    Example

    android:maxLength="10"
    
    0 讨论(0)
提交回复
热议问题