【问题标题】:android EditText maxLength not workingandroid EditText maxLength 不起作用
【发布时间】:2015-08-26 18:07:15
【问题描述】:

这是我的xml

<EditText
android:id="@+id/et_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions|textVisiblePassword"
android:hint="Provide comments here..."
android:gravity="top"
android:maxLength="5"
android:textSize="12sp"
android:visibility="visible"
/>

使用此代码也不行

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

maxLenth 不工作,我不知道为什么,但它不工作。
我已经检查了堆栈上的其他答案,但它们也不起作用。
请检查是否有任何 EditText 属性冲突或有什么问题?

编辑:其他开发人员也面临同样的问题
见 cmets here Mansi 和 aat 面临同样的问题
而在 cmets 中 here Vincy 和 Riser 也面临同样的问题

编辑:问题已解决
我正在使用输入过滤器,它覆盖了 xml 中的最大长度,使其无法工作。
输入过滤器对我不起作用的原因是我使用了另一个输入过滤器,它覆盖了以前的 maxLength 输入过滤器。
把它变成一个单一的输入过滤器为我解决了这个问题。

【问题讨论】:

  • 它工作正常,android:maxLength = "5" 工作!
  • 但这对我不起作用:/
  • 我建议你用USB调试在手机上检查
  • 这就是我正在做的事情!
  • 告诉我们您所说的“不工作”是什么意思?可能是截图什么的

标签: java android filter android-edittext maxlength


【解决方案1】:

试试这个,它适用于最大长度和输入过滤器

month.setFilters(new InputFilter[]{new InputFilterMinMax("0", "12"), new InputFilter.LengthFilter(2)});

【讨论】:

  • 这一行很有帮助 month.setFilters(new InputFilter[]new InputFilter.LengthFilter(2)});
  • 特别是如果您想应用多个过滤器
  • 对于 xamarin 是:新的 Android.Text.InputFilterLengthFilter(2)
【解决方案2】:

相当老的帖子,但是,我注意到 XML 是一个实际的 EditText 对象,而您将过滤器添加到 TextView 可以与 EditText 不同地处理它。如果您手动添加 InputFilter 对象,则 xml 属性将被覆盖。

将InputFilters 添加到View 的示例代码似乎是TextView 对象。如果您手动添加过滤器,请确保您拉出正确的视图并将其转换为EditText - 它现在对我有用。

祝你好运。

【讨论】:

    【解决方案3】:

    如果您对编辑文本使用 InputFilter,则 maxLength 将不起作用。

    【讨论】:

      【解决方案4】:

      如果您已经有 InputFilter,那么 maxLength 将不起作用。您必须创建一个额外的 InputFilter 并添加它:

          // Filter for comma character
          String blockCharacterSet = ",";
          InputFilter filter = (source, start, end, dest, dstart, dend) -> {
              if (source != null && blockCharacterSet.contains(("" + source))) {
                  return "";
              }
              return null;
          };
      
          // Filter for max Length
          InputFilter filter1 = new InputFilter.LengthFilter(20);
      
          // Set the filters
          et_list_name.setFilters(new InputFilter[] { filter, filter1 });
      

      【讨论】:

        【解决方案5】:

        添加字符限制和输入限制。

           private String BlockCharacterSet_Name = "\\@$#!=><&^*+\"\'";   
        
         mFirstName.setFilters(new InputFilter[] {new InputFilter.LengthFilter(25),inputFilter_Name});
        
        
        
         private InputFilter inputFilter_Name = new InputFilter() { //filter input for name fields
                @Override
                public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
                    if (charSequence != null && BlockCharacterSet_Name.contains(("" + charSequence))) {
                        return "";
                    }
                    return null;
                }
            };
        

        {new InputFilter.LengthFilter(25),inputFilter_Name}

        允许您在同一个 InputFilter 中同时设置最大长度限制和特殊字符限制,因此您不需要为相同的 InputFilter 创建单独的 Inputfilter。

        【讨论】:

          【解决方案6】:

          试试这个:

          val existingFilters: Array<InputFilter> = editText.filters
          editText.filters = existingFilters.apply { plus(filter) }
          

          您需要获取现有过滤器的列表,例如通过 XML 应用的 maxLength,然后向其中添加更多过滤器。在您的情况下,editText 的 maxLength 属性被您尝试应用的自定义 InputFilter 覆盖。

          【讨论】:

            【解决方案7】:

            在我的例子中使用了 DataBinding,但有人使用 ViewBinding 在代码中更改了filter。

            binding.edit_text.filters = arrayOf<InputFilter>(object : InputFilter {
                override fun filter(
                    source: CharSequence?,
                    start: Int,
                    end: Int,
                    dest: Spanned?,
                    dstart: Int,
                    dend: Int
                ): CharSequence {
                    return source.toString().filter { it.toString().matches(("[${LETTERS}]").toRegex()) }
                }
            })
            

            在我从 EditText 中删除该 id 之前,Android Studio 没有找到引用“@+id/edit_text”的方法。

            【讨论】:

              【解决方案8】:

              如果您对编辑文本使用 InputFilter,则 maxLength 将不起作用。

              如果你添加一个过滤器替换原来的过滤器是正确的。

              您可以在 xml 中使用 android:maxLength="5"。

              在您的代码中只需添加一个过滤器而不替换现有的过滤器。

              科特林:

              editText.filters = arrayOf(
                *editText.filters,
                InputFilter.AllCaps(), // your filter
                // other filters
              )
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2019-01-10
                • 2021-11-30
                • 2012-10-01
                • 2018-12-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多