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

后端 未结 22 1865
囚心锁ツ
囚心锁ツ 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:16

    Kotlin one-liner

    etxt_userinput.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(100))
    

    where 100 is the maxLength

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

    I had saw a lot of good solutions, but I'd like to give a what I think as more complete and user-friendly solution, which include:

    1, Limit length.
    2, If input more, give a callback to trigger your toast.
    3, Cursor can be at middle or tail.
    4, User can input by paste a string.
    5, Always discard overflow input and keep origin.

    public class LimitTextWatcher implements TextWatcher {
    
        public interface IF_callback{
            void callback(int left);
        }
    
        public IF_callback if_callback;
    
        EditText editText;
        int maxLength;
    
        int cursorPositionLast;
        String textLast;
        boolean bypass;
    
        public LimitTextWatcher(EditText editText, int maxLength, IF_callback if_callback) {
    
            this.editText = editText;
            this.maxLength = maxLength;
            this.if_callback = if_callback;
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            if (bypass) {
    
                bypass = false;
    
            } else {
    
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(s);
                textLast = stringBuilder.toString();
    
                this.cursorPositionLast = editText.getSelectionStart();
            }
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
    
        }
    
        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() > maxLength) {
    
                int left = maxLength - s.toString().length();
    
                bypass = true;
                s.clear();
    
                bypass = true;
                s.append(textLast);
    
                editText.setSelection(this.cursorPositionLast);
    
                if (if_callback != null) {
                    if_callback.callback(left);
                }
            }
    
        }
    
    }
    
    
    edit_text.addTextChangedListener(new LimitTextWatcher(edit_text, MAX_LENGTH, new LimitTextWatcher.IF_callback() {
        @Override
        public void callback(int left) {
            if(left <= 0) {
                Toast.makeText(MainActivity.this, "input is full", Toast.LENGTH_SHORT).show();
            }
        }
    }));
    

    What I failed to do is, if user highlight a part of the current input and try to paste an very long string, I don't know how to restore the highlight.

    Such as, max length is set to 10, user inputed '12345678', and mark '345' as highlight, and try to paste a string of '0000' which will exceed limitation.

    When I try to use edit_text.setSelection(start=2, end=4) to restore origin status, the result is, it just insert 2 space as '12 345 678', not the origin highlight. I'd like someone solve that.

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

    Another way you can achieve this is by adding the following definition to the XML file:

    <EditText
        android:id="@+id/input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="6"
        android:hint="@string/hint_gov"
        android:layout_weight="1"/>
    

    This will limit the maximum length of the EditText widget to 6 characters.

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

    it simple way in xml:

    android:maxLength="4"
    

    if u require to set 4 character in xml edit-text so,use this

    <EditText
        android:id="@+id/edtUserCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLength="4"
        android:hint="Enter user code" />
    
    0 讨论(0)
  • 2020-11-22 14:23
    TextView tv = new TextView(this);
    tv.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(250) });
    
    0 讨论(0)
  • 2020-11-22 14:24

    Xml

    android:maxLength="10"
    

    Java:

    InputFilter[] editFilters = editText.getFilters();
    InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
    System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
    newFilters[editFilters.length] = new InputFilter.LengthFilter(maxLength);
    editText.setFilters(newFilters);
    

    Kotlin:

    editText.filters += InputFilter.LengthFilter(maxLength)
    
    0 讨论(0)
提交回复
热议问题