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

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

    A note to people who are already using a custom input filter and also want to limit the max length:

    When you assign input filters in code all previously set input filters are cleared, including one set with android:maxLength. I found this out when attempting to use a custom input filter to prevent the use of some characters that we don't allow in a password field. After setting that filter with setFilters the maxLength was no longer observed. The solution was to set maxLength and my custom filter together programmatically. Something like this:

    myEditText.setFilters(new InputFilter[] {
            new PasswordCharFilter(), new InputFilter.LengthFilter(20)
    });
    
    0 讨论(0)
  • 2020-11-22 14:37

    For anyone else wondering how to achieve this, here is my extended EditText class EditTextNumeric.

    .setMaxLength(int) - sets maximum number of digits

    .setMaxValue(int) - limit maximum integer value

    .setMin(int) - limit minimum integer value

    .getValue() - get integer value

    import android.content.Context;
    import android.text.InputFilter;
    import android.text.InputType;
    import android.widget.EditText;
    
    public class EditTextNumeric extends EditText {
        protected int max_value = Integer.MAX_VALUE;
        protected int min_value = Integer.MIN_VALUE;
    
        // constructor
        public EditTextNumeric(Context context) {
            super(context);
            this.setInputType(InputType.TYPE_CLASS_NUMBER);
        }
    
        // checks whether the limits are set and corrects them if not within limits
        @Override
        protected void onTextChanged(CharSequence text, int start, int before, int after) {
            if (max_value != Integer.MAX_VALUE) {
                try {
                    if (Integer.parseInt(this.getText().toString()) > max_value) {
                        // change value and keep cursor position
                        int selection = this.getSelectionStart();
                        this.setText(String.valueOf(max_value));
                        if (selection >= this.getText().toString().length()) {
                            selection = this.getText().toString().length();
                        }
                        this.setSelection(selection);
                    }
                } catch (NumberFormatException exception) {
                    super.onTextChanged(text, start, before, after);
                }
            }
            if (min_value != Integer.MIN_VALUE) {
                try {
                    if (Integer.parseInt(this.getText().toString()) < min_value) {
                        // change value and keep cursor position
                        int selection = this.getSelectionStart();
                        this.setText(String.valueOf(min_value));
                        if (selection >= this.getText().toString().length()) {
                            selection = this.getText().toString().length();
                        }
                        this.setSelection(selection);
                    }
                } catch (NumberFormatException exception) {
                    super.onTextChanged(text, start, before, after);
                }
            }
            super.onTextChanged(text, start, before, after);
        }
    
        // set the max number of digits the user can enter
        public void setMaxLength(int length) {
            InputFilter[] FilterArray = new InputFilter[1];
            FilterArray[0] = new InputFilter.LengthFilter(length);
            this.setFilters(FilterArray);
        }
    
        // set the maximum integer value the user can enter.
        // if exeeded, input value will become equal to the set limit
        public void setMaxValue(int value) {
            max_value = value;
        }
        // set the minimum integer value the user can enter.
        // if entered value is inferior, input value will become equal to the set limit
        public void setMinValue(int value) {
            min_value = value;
        }
    
        // returns integer value or 0 if errorous value
        public int getValue() {
            try {
                return Integer.parseInt(this.getText().toString());
            } catch (NumberFormatException exception) {
                return 0;
            }
        }
    }
    

    Example usage:

    final EditTextNumeric input = new EditTextNumeric(this);
    input.setMaxLength(5);
    input.setMaxValue(total_pages);
    input.setMinValue(1);
    

    All other methods and attributes that apply to EditText, of course work too.

    0 讨论(0)
  • 2020-11-22 14:38
    EditText editText = new EditText(this);
    int maxLength = 3;    
    editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
    
    0 讨论(0)
  • 2020-11-22 14:39

    Try this for Java programmatically:

    myEditText(new InputFilter[] {new InputFilter.LengthFilter(CUSTOM_MAX_LEN)});
    
    0 讨论(0)
提交回复
热议问题