【问题标题】:EditText maxLength property not applied properly when setFilters is used使用 setFilters 时未正确应用 EditText maxLength 属性
【发布时间】:2012-09-01 06:10:43
【问题描述】:

当我在 EditText 上使用 setFilter 方法来处理特殊字符时,maxLength 属性无法按预期工作。我的代码如下。

editName = (EditText)findViewById(R.id.rna_editTextName);
        editName.setFilters(new InputFilter[]{getFilteredChars()}); 


        //Below method returns filtered characters.
       public InputFilter getFilteredChars() 
       {
           InputFilter filter = new InputFilter() 
            { 
                 @Override
                    public CharSequence filter(CharSequence source, int start, int end,                                Spanned dest, int dstart, int dend) {
                        if (end > start) {

                            char[] acceptedChars = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
                                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ' ,'.', '\''};

                            for (int index = start; index < end; index++) {                                         
                                if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) { 
                                    return ""; 
                                }               
                            }
                        }
                        return null;
                } 
        };
        return filter;
       }

【问题讨论】:

  • 定义“未按预期工作”
  • 我想处理我在上面添加的方法即 getfilteredChars() 的 edittext gor 上的特殊字符问题。通过使用这种方法可以处理特殊字符问题,但因为该编辑文本接受无限字符。在以编程方式设置edittext的maxlenth属性时,我们使用InputFilters,在getfilters中我们也使用Inputfilters,所以我认为该maxlenth属性不起作用。所以告诉我解决这个问题的方法。
  • 问题的任何解决方案
  • 直到我没有得到任何解决方案。

标签: android android-edittext input-filter


【解决方案1】:

您可以在数组中添加多个过滤器。 比如你的editText的最大字符限制和特殊字符过滤器。

editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAX_CHARACTER_LIMIT),SpecialCharacterInputFilter});

【讨论】:

    【解决方案2】:

    试试这个代码。基于@Jozua 的回答

        /**
     * Adds filter to EditText preserving other filters.
     * 
     * @param editText
     * @param filter
     */
    public static void setFilter(EditText editText, InputFilter filter) {
    InputFilter curFilters[] = editText.getFilters();
    
    if (curFilters != null) {
        InputFilter newFilters[] = new InputFilter[curFilters.length + 1];
        System.arraycopy(curFilters, 0, newFilters, 0, curFilters.length);
        newFilters[curFilters.length] = filter;
        editText.setFilters(newFilters);
    } else {
        editText.setFilters(new InputFilter[] { filter });
    }
    }
    

    【讨论】:

      【解决方案3】:

      以编程方式设置edittext的maxlenth属性时

      请显示使用代码。

      Wild Guess:问题可能是您在布局中设置了 maxLength。通过调用 setFilters(),此行为将被您的过滤器替换。

      解决方案: 使用多个过滤器或在 getFilteredChars() 过滤器中实现 maxLenght 行为。

      编辑: 你可能想看看http://developer.android.com/reference/android/text/InputFilter.LengthFilter.html

      对于您在评论中提出的问题, 来自文档:

      when the buffer is going to replace the range dstart … dend of dest with the new text from the range start … end of source
      

      所以,类似于(伪代码 liveCoding):

      dest.lenght - (dend-dstart) + (end-start)  = new legnth
      

      【讨论】:

      • 但是如何在您的 getFilteredChars() 过滤器中实现 maxLenght 行为?请解释一下。
      【解决方案4】:

      这是因为maxLength 属性在您的EditText 上设置了InputFilter。通过调用 EditText.setFilters(new InputFilter[] {&lt;YOUR_FILTER&gt;}),您将覆盖所有现有的 InputFilters,包括 maxLength 使用的那个。

      要解决此问题,请复制 EditText.getFilters() 返回的数组并将您自己的数组添加到其中:

      InputFilter[] editFilters = edit.getFilters();
      InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
      System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
      newFilters[editFilters.length] = <YOUR_FILTER>;
      edit.setFilters(newFilters);
      

      【讨论】:

        猜你喜欢
        • 2016-02-28
        • 2021-05-29
        • 2017-07-27
        • 1970-01-01
        • 1970-01-01
        • 2015-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多