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

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

    XML

    android:maxLength="10"

    Programmatically:

    int maxLength = 10;
    InputFilter[] filters = new InputFilter[1];
    filters[0] = new InputFilter.LengthFilter(maxLength);
    yourEditText.setFilters(filters);
    

    Note: internally, EditText & TextView parse the value of android:maxLength in XML and use InputFilter.LengthFilter() to apply it.

    See: TextView.java#L1564

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

    You can use android:maxLength="10" in the EditText.(Here the limit is upto 10 characters)

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

    Kotlin:

    edit_text.filters += InputFilter.LengthFilter(10)
    

    ZTE Blade A520 has strange effect. When you type more than 10 symbols (for instance, 15), EditText shows first 10, but other 5 are not visible and not accessible. But when you delete symbols with Backspace, it first deletes right 5 symbols and then removes remaining 10. To overcome this behaviour use a solution:

    android:inputType="textNoSuggestions|textVisiblePassword"
    android:maxLength="10"
    

    or this:

    android:inputType="textNoSuggestions"
    

    or this, if you want to have suggestions:

    private class EditTextWatcher(private val view: EditText) : TextWatcher {
        private var position = 0
        private var oldText = ""
    
        override fun afterTextChanged(s: Editable?) = Unit
    
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            oldText = s?.toString() ?: ""
            position = view.selectionStart
        }
    
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            val newText = s?.toString() ?: ""
            if (newText.length > 10) {
                with(view) {
                    setText(oldText)
                    position = if (start > 0 && count > 2) {
                        // Text paste in nonempty field.
                        start
                    } else {
                        if (position in 1..10 + 1) {
                            // Symbol paste in the beginning or middle of the field.
                            position - 1
                        } else {
                            if (start > 0) {
                                // Adding symbol to the end of the field.
                                start - 1
                            } else {
                                // Text paste in the empty field.
                                0
                            }
                        }
                    }
                    setSelection(position)
                }
            }
        }
    }
    
    // Usage:
    editTextWatcher = EditTextWatcher(view.edit_text)
    view.edit_text.addTextChangedListener(editTextWatcher)
    
    0 讨论(0)
  • 2020-11-22 14:26

    use an input filter to limit the max length of a text view.

    TextView editEntryView = new TextView(...);
    InputFilter[] filterArray = new InputFilter[1];
    filterArray[0] = new InputFilter.LengthFilter(8);
    editEntryView.setFilters(filterArray);
    
    0 讨论(0)
  • 2020-11-22 14:26
    //Set Length filter. Restricting to 10 characters only
    editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAX_LENGTH)});
    
    //Allowing only upper case characters
    editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
    
    //Attaching multiple filters
    editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAX_LENGTH), new InputFilter.AllCaps()});
    
    0 讨论(0)
  • 2020-11-22 14:27

    I have had this problem and I consider we are missing a well explained way of doing this programmatically without losing the already set filters.

    Setting the length in XML:

    As the accepted answer states correctly, if you want to define a fixed length to an EditText which you won't change further in the future just define in your EditText XML:

    android:maxLength="10"     
    

    Setting the length programmatically

    To set the length programmatically you'll need to set it through an InputFilter. But if you create a new InputFilter and set it to the EditText you will lose all the other already defined filters (e.g. maxLines, inputType, etc) which you might have added either through XML or programatically.

    So this is WRONG:

    editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
    

    To avoid losing previously added filters you need to get those filters, add the new one (maxLength in this case), and set the filters back to the EditText as follow:

    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 however made it easier for everyone, you also need to add the filter to the already existing ones but you can achieve that with a simple:

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