【问题标题】:What's the best way to limit text length of EditText in Android在Android中限制EditText文本长度的最佳方法是什么
【发布时间】:2011-03-18 03:46:49
【问题描述】:

在 Android 中限制 EditText 文本长度的最佳方法是什么?

有没有办法通过 xml 做到这一点?

【问题讨论】:

  • 我想为我的 EditText 设置最大字符数。起初,文本长度限制是一回事并不明显。 (只是给另一个困惑的旅行者的便条)。
  • 正确答案在这里stackoverflow.com/a/19222238/276949。这个答案限制了长度并防止缓冲区在达到限制后不断填充,因此允许您的 退格键 正常工作。

标签: android android-edittext maxlength


【解决方案1】:

Documentation

Example

android:maxLength="10"

【讨论】:

  • 天啊!这样的事情我也经历过。我在看代码,没有 setMaxLength 方法。
  • 这里的 Check 有什么作用?它只是链接到这个页面。
  • @Vincy,你错了。 maxLength 属性仍然有效。
  • 请注意android:maxLength 等同于InputFilter.LengthFilter,因此当以编程方式更改其过滤器时,您也修改了其XML 过滤器。
  • 对于那些说它不起作用的人,请注意调用setFilters 将停止android:maxLength 工作,因为它会覆盖XML 设置的过滤器。换句话说,如果您以编程方式设置任何过滤器,则必须以编程方式设置它们。
【解决方案2】:

使用输入过滤器来限制文本视图的最大长度。

TextView editEntryView = new TextView(...);
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(8);
editEntryView.setFilters(filterArray);

【讨论】:

  • 如果有人已经制作了一些InputFilter,这将非常有用。它覆盖了xml文件中的android:maxlength,所以我们需要这样添加LengthFilter
  • 我认为你的答案是最好的答案,因为它更有活力,+1
【解决方案3】:
EditText editText = new EditText(this);
int maxLength = 3;    
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});

【讨论】:

  • 这类似于android使用xml的方式。
  • 如何设置最小长度?
  • @AkhilaMadari,可能是TextWatcher
【解决方案4】:

给已经在使用自定义输入过滤器并且想要限制最大长度的人的说明:

当您在代码中分配输入过滤器时,所有先前设置的输入过滤器都会被清除,包括带有android:maxLength 的一组。我在尝试使用自定义输入过滤器来防止在密码字段中使用我们不允许的某些字符时发现了这一点。使用 setFilters 设置该过滤器后,不再观察到 maxLength。解决方案是以编程方式将 maxLength 和我的自定义过滤器设置在一起。像这样的:

myEditText.setFilters(new InputFilter[] {
        new PasswordCharFilter(), new InputFilter.LengthFilter(20)
});

【讨论】:

  • 您可以先检索现有过滤器,例如 InputFilter[] existingFilters = editText.getFilters();然后将您的过滤器与现有的过滤器一起添加
【解决方案5】:

我遇到了这个问题,我认为我们缺少一种很好解释的方式来以编程方式执行此操作而不会丢失已设置的过滤器。

在 XML 中设置长度:

正如接受的答案正确说明的那样,如果您想为 EditText 定义一个固定长度,以后您不会进一步更改,只需在您的 EditText XML 中定义:

android:maxLength="10"     

以编程方式设置长度

要以编程方式设置长度,您需要通过InputFilter 进行设置。但是,如果您创建一个新的 InputFilter 并将其设置为 EditText,您将丢失您可能通过 XML 或以编程方式添加的所有其他已定义的过滤器(例如 maxLines、inputType 等)。

所以这是错误

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

为避免丢失以前添加的过滤器,您需要获取这些过滤器,添加新过滤器(在本例中为 maxLength),并将过滤器设置回EditText,如下所示:

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)

【讨论】:

  • +1。不知道过滤器。 Otoh,在比较了 Java 和 Kotlin 代码之后,我想知道为什么人们一直坚持使用 Java 进行 Android 开发 xd。
【解决方案6】:
TextView tv = new TextView(this);
tv.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(250) });

【讨论】:

  • 当然,只适用于安卓
【解决方案7】:

对于其他想知道如何实现这一点的人,这是我的扩展 EditTextEditTextNumeric

.setMaxLength(int) - 设置最大位数

.setMaxValue(int) - 限制最大整数值

.setMin(int) - 限制最小整数值

.getValue() - 获取整数值

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

示例用法:

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

适用于EditText 的所有其他方法和属性当然也可以。

【讨论】:

  • 如果我们在xml布局中添加这个会更好。使用 xml 时出现错误原因:android.view.InflateException: Binary XML file line #47: Error inflating class com.passenger.ucabs.utils.EditTextNumeric
  • @Martynas 遇到了和 Shihab_returns 一样的错误,有什么解决办法吗?
【解决方案8】:

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

科特林:

editText.filters += InputFilter.LengthFilter(maxLength)

【讨论】:

    【解决方案9】:

    由于 goto10 的观察,我整理了以下代码,以防止通过设置最大长度而丢失其他过滤器:

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

    【讨论】:

    • 您可能需要稍微更新一下代码。分配的数组大小必须是 curFilters.length + 1,并且在创建 newFilters 后,您没有将“this”设置为新分配的数组。 InputFilter newFilters[] = new InputFilter[curFilters.length + 1]; System.arraycopy(curFilters, 0, newFilters, 0, curFilters.length); this.setFilters(newFilters);
    【解决方案10】:
    //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()});
    

    【讨论】:

      【解决方案11】:

      实现此目的的另一种方法是将以下定义添加到 XML 文件中:

      <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"/>
      

      这会将EditText 小部件的最大长度限制为 6 个字符。

      【讨论】:

        【解决方案12】:

        XML

        android:maxLength="10"

        以编程方式:

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

        注意:在内部,EditText & TextView 会解析 XML 中 android:maxLength 的值,并使用 InputFilter.LengthFilter() 来应用它。

        见:TextView.java#L1564

        【讨论】:

          【解决方案13】:

          material.io,您可以使用TextInputEditText 结合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>
          

          你可以用drawable配置一个密码EditText:

          或者您可以使用/不使用计数器来限制文本长度:

          依赖:

          implementation 'com.google.android.material:material:1.1.0-alpha02'
          

          【讨论】:

            【解决方案14】:

            xml 中的简单方法:

            android:maxLength="4"
            

            如果您需要在 xml 编辑文本中设置 4 个字符,请使用此

            <EditText
                android:id="@+id/edtUserCode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxLength="4"
                android:hint="Enter user code" />
            

            【讨论】:

              【解决方案15】:

              以编程方式尝试 Java

              myEditText(new InputFilter[] {new InputFilter.LengthFilter(CUSTOM_MAX_LEN)});
              

              【讨论】:

              • 您忘记调用该方法,所以它应该看起来像:myEditText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(CUSTOM_MAX_LEN)});
              【解决方案16】:

              这是一个自定义的 EditText 类,它允许 Length 过滤器与其他过滤器一起使用。 感谢 Tim Gallagher 的回答(下)

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

              【讨论】:

                【解决方案17】:

                这很好用...

                android:maxLength="10"

                这将只接受10 字符。

                【讨论】:

                  【解决方案18】:

                  科特林:

                  edit_text.filters += InputFilter.LengthFilter(10)
                  

                  ZTE Blade A520 有奇怪的效果。当您键入超过 10 个符号(例如 15 个)时,EditText 显示前 10 个,但其他 5 个不可见且不可访问。但是,当您使用Backspace 删除符号时,它首先删除右 5 个符号,然后删除剩余的 10 个。要克服此行为,请使用 a solution

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

                  或者这个:

                  android:inputType="textNoSuggestions"
                  

                  或者这个,如果你想有建议的话:

                  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)
                  

                  【讨论】:

                    【解决方案19】:

                    xml 中的简单方法:

                    android:maxLength="@{length}"
                    

                    要以编程方式设置它,您可以使用以下函数

                    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()]));
                        }
                    }
                    

                    【讨论】:

                      【解决方案20】:

                      我看到了很多好的解决方案,但我想给出一个我认为更完整和用户友好的解决方案,其中包括:

                      1,限制长度。
                      2,如果输入更多,给一个回调来触发你的吐司。
                      3、光标可以在中间或尾部。
                      4、用户可以通过粘贴字符串的方式输入。
                      5、始终丢弃溢出输入并保持原点。

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

                      我没有做的是,如果用户突出显示当前输入的一部分并尝试粘贴一个很长的字符串,我不知道如何恢复突出显示。

                      例如,最大长度设置为10,用户输入'12345678',并将'345'标记为突出显示,并尝试粘贴'0000'的字符串,这将超出限制。

                      当我尝试使用 edit_text.setSelection(start=2, end=4) 恢复原点状态时,结果是,它只是插入 2 个空格作为 '12 345 678',而不是原点突出显示。我希望有人能解决这个问题。

                      【讨论】:

                        【解决方案21】:

                        您可以在 EditText 中使用android:maxLength="10"。(此处限制为最多 10 个字符)

                        【讨论】:

                          猜你喜欢
                          • 2010-10-13
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 2020-03-03
                          • 2010-12-14
                          • 2013-03-10
                          • 1970-01-01
                          相关资源
                          最近更新 更多