Android 如何在代码中获取EditText maxLength设置

Android 如何在代码中获取EditText maxLength设置,android,android-edittext,Android,Android Edittext,我希望在运行时查看EditText的maxLength,以便能够做出文本显示决定。 可能吗 这是我不想做的事情的描述 我有一个多行的列表视图,每行有一个EditText和一个TextView。 我已经创建了ArrayAdapter的一个子类,以便能够提供我要放置在每行EditText中的字符串。 我已经在XML文件中设置了android:maxLength=“12”。 我想在EditText字段中显示一个数字,但是如果我想显示的数字超过了android:maxLength=“12”我想显示一条“

我希望在运行时查看
EditText
maxLength
,以便能够做出文本显示决定。
可能吗

这是我不想做的事情的描述

我有一个多行的列表视图,每行有一个EditText和一个TextView。
我已经创建了ArrayAdapter的一个子类,以便能够提供我要放置在每行EditText中的字符串。
我已经在XML文件中设置了android:maxLength=“12”。
我想在EditText字段中显示一个数字,但是如果我想显示的数字超过了android:maxLength=“12”我想显示一条“错误消息”

我不想在ArrayAdapter的子类中硬编码12

可能有一个简单的解决方案,但我还没有找到。
(android第一次…

这应该可以:


有点复杂,但我不知道还有其他方法。我希望它能工作(未经测试):


只有有限的参数有它们的getter,所以我认为您无法读取它

所以在values文件夹中写入长度(比如12),并在xml布局和arrayAdapter中使用它。 现在它不是硬编码的

1)在值中创建integer.xml*


您可以使用反射API获取字段值

为什么你不应该这么做
几乎每个人都会反对它(包括我),因为:

  • 很慢
  • 它取决于实施
  • 它不打算被访问(显然)
到目前为止,看看源代码(Android API 19),实现取决于
InputFilter.LengthFilter
在构造函数中设置为:

if (maxlength >= 0) {
    setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxlength) });
} else {
    setFilters(NO_FILTERS);
}
其中,
maxLength
是您感兴趣的整数,从xml属性解析(
android:maxLength=“@Integer/max_length”
)。 此
InputFilter.LengthFilter
只有一个字段(
private int mMax
),并且没有访问器方法

如何做到这一点
  • 在相关实用程序类中声明一个静态方法,该方法接受
    TextView
    并返回
    int
  • 迭代
    TextView
    上的每个
    InputFilter
    集,并找到属于
    InputFilter.LengthFilter
    实现的一个
  • 使用反射访问、获取并返回mMax字段的值
这会给你这样的东西:

import java.lang.reflect.Field;

// [...]

public static int getMaxLengthForTextView(TextView textView)
{
    int maxLength = -1;

    for (InputFilter filter : textView.getFilters()) {
        if (filter instanceof InputFilter.LengthFilter) {
            try {
                Field maxLengthField = filter.getClass().getDeclaredField("mMax");
                maxLengthField.setAccessible(true);

                if (maxLengthField.isAccessible()) {
                    maxLength = maxLengthField.getInt(filter);
                }
            } catch (IllegalAccessException e) {
                Log.w(filter.getClass().getName(), e);
            } catch (IllegalArgumentException e) {
                Log.w(filter.getClass().getName(), e);
            } catch (NoSuchFieldException e) {
                Log.w(filter.getClass().getName(), e);
            } // if an Exception is thrown, Log it and return -1
        }
    }

return maxLength;
}

如前所述,如果设置
TextView
最大长度的实现发生更改,则会中断。当方法开始抛出时,将通知您此更改。即使这样,该方法仍然返回-1,您应该将其作为无限长度处理。

扩展编辑文本并从构造函数中的attributeset中检索值

公共类MyEditText扩展了EditText{

public static final String XML_NAMESPACE_ANDROID = "http://schemas.android.com/apk/res/android";
private int mMaxLength;

public MyEditText(Context context) {
    super(context, null);
}

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    mMaxLength = attrs.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", -1);
}

在api 21中,您可以这样做:

for (InputFilter filter : mEditText.getFilters()) {
        if (filter instanceof InputFilter.LengthFilter) {
            ((InputFilter.LengthFilter) filter).getMax());
        }
}

我希望这对其他人有所帮助。

Kotlin单行解决方案-如果未设置,则返回最大长度或null

view.filters.filterIsInstance<InputFilter.LengthFilter>().firstOrNull()?.max
view.filters.filterIsInstance().firstOrNull()?.max
作为扩展:

val TextView.maxLength: Int?
    get() = filters.filterIsInstance<InputFilter.LengthFilter>().firstOrNull()?.max
val TextView.maxLength:Int?
get()=过滤器.filterIsInstance().firstOrNull()?.max

谢谢,这让我走上了正确的道路,但我如何才能找回12。我的尝试已经进行了一半,但我遗漏了一些东西。我找回了方法调用的默认值,而不是实际值。我感觉需要归结到这个……选项。我希望找到一种更具活力的方式。我通常都是通过这种方式,无论在哪里很难找回值。
import java.lang.reflect.Field;

// [...]

public static int getMaxLengthForTextView(TextView textView)
{
    int maxLength = -1;

    for (InputFilter filter : textView.getFilters()) {
        if (filter instanceof InputFilter.LengthFilter) {
            try {
                Field maxLengthField = filter.getClass().getDeclaredField("mMax");
                maxLengthField.setAccessible(true);

                if (maxLengthField.isAccessible()) {
                    maxLength = maxLengthField.getInt(filter);
                }
            } catch (IllegalAccessException e) {
                Log.w(filter.getClass().getName(), e);
            } catch (IllegalArgumentException e) {
                Log.w(filter.getClass().getName(), e);
            } catch (NoSuchFieldException e) {
                Log.w(filter.getClass().getName(), e);
            } // if an Exception is thrown, Log it and return -1
        }
    }

return maxLength;
}
public static final String XML_NAMESPACE_ANDROID = "http://schemas.android.com/apk/res/android";
private int mMaxLength;

public MyEditText(Context context) {
    super(context, null);
}

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    mMaxLength = attrs.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", -1);
}
for (InputFilter filter : mEditText.getFilters()) {
        if (filter instanceof InputFilter.LengthFilter) {
            ((InputFilter.LengthFilter) filter).getMax());
        }
}
view.filters.filterIsInstance<InputFilter.LengthFilter>().firstOrNull()?.max
val TextView.maxLength: Int?
    get() = filters.filterIsInstance<InputFilter.LengthFilter>().firstOrNull()?.max