Android:: 使用其他 InputFilter 以编程方式设置 EditText 的最大长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24749833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:41:57  来源:igfitidea点击:

Android:: Set max-length of EditText programmatically with other InputFilter

android

提问by pokoso

I'm using InputFilter like this to allow only alpha and numbers

我像这样使用 InputFilter 只允许字母和数字

private InputFilter[] inputFilters = new InputFilter[] { new InputFilter()
{
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
    {
        for (int i = start; i < end; ++i)
        {
            if (!Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890]*").matcher(String.valueOf(source.charAt(i))).matches())
            {
                return "";
            }
        }

        return null;
    }
} };

But problem is "android:maxLength" value in xml file is not working with this InputFilter

但问题是 xml 文件中的“android:maxLength”值不适用于此 InputFilter

I think I need code in InputFilter to set max-length of EditText

我想我需要 InputFilter 中的代码来设置 EditText 的最大长度

Anyone has good idea for this?

有人对此有什么好主意吗?

Thanks

谢谢

采纳答案by Biraj Zalavadia

Just try this way

试试这个方法

InputFilter

输入过滤器

InputFilter filter = new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; ++i)
            {
                if (!Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890]*").matcher(String.valueOf(source.charAt(i))).matches())
                {
                    return "";
                }
            }

            return null;
        }
    };

How to apply

怎样申请

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText edt =(EditText)findViewById(R.id.edt) ;

        edt.setFilters(new InputFilter[]{filter,new InputFilter.LengthFilter(10)});


    }

回答by Deepak

public void setEditTextMaxLength(int length) {
    InputFilter[] filterArray = new InputFilter[1];
    filterArray[0] = new InputFilter.LengthFilter(length);
    edt_text.setFilters(filterArray);
}

回答by Divins Mathew

A simple one-liner would be:

一个简单的单行是:

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

//replace 10 with required length.

回答by IntelliJ Amiya

If we want to limit the character input in an EditText, EditTextin XML layout gives us android:maxLengthto do this. But in java codes you might wonder why there isn't any setMaxLength(int length) function. The reason behind this is that when you want to restrict the EditText to accept certain value, you have to filter them and this would be invoked by setFilters. To make our EditText to have a fixed size we can use the following code.

如果我们想限制EditText 中的字符输入,EditText在 XML 布局中我们android:maxLength可以这样做。但是在 Java 代码中,您可能想知道为什么没有任何setMaxLength(int length) 函数。这背后的原因是,当你想限制 EditText 接受某个值时,你必须过滤它们,这将由 setFilters 调用。为了使我们的 EditText 具有固定大小,我们可以使用以下代码。

EditText et = new EditText(this);
int maxLength = 3;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
et.setFilters(FilterArray);

回答by Ray Hunter

Building on @Deepak's I used the following:

在@Deepak 的基础上,我使用了以下内容:

public void setEditTextMaxLength(final EditText editText, int length) {
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(length);
    editText.setFilters(FilterArray);
}

回答by Milad Mohamadi

You can use this function very very easy :

您可以非常轻松地使用此功能:

 public static void setEditTextMaxLength(EditText editText, int length) {
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(length);
    editText.setFilters(FilterArray);
  }

and use this on your activity:

并在您的活动中使用它:

setEditTextMaxLength(edt_passcode, 5);

回答by Biplob Das

For Kotlin

myEditText.filters = arrayOf(InputFilter.LengthFilter(maxLength))

对于科特林

myEditText.filters = arrayOf(InputFilter.LengthFilter(maxLength))