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

时间:2011-11-09 17:29:07

标签: android android-edittext

我希望在运行时看到maxLength的{​​{1}}能够做出文本显示决定。
这可能吗?

以下是我不想做的事情的描述。

我有一个包含许多行的ListView,每行都有一个EditText和一个TextView 我已经创建了一个ArrayAdapter的子类,以便能够提供我想要放在每行EditText中的String。
我在XML文件中设置了EditText 我想在EditText字段中显示一个数字,但如果我要显示的数字超过android:maxLength="12",我想改为显示“错误消息”。

我宁愿不在我的ArrayAdapter子类中硬编码那个12。

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

7 个答案:

答案 0 :(得分:35)

这应该有效:

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

答案 1 :(得分:32)

只有有限的参数才有吸气剂,所以我认为你不能读它。

因此在values文件夹中写入长度(Say 12)并在xml layout和arrayAdapter中使用它。 现在它没有硬编码。

1)在值*

中创建integer.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="integer" name="max_length">12</item>
</resources>

2)布局

<TextView  android:id="@+id/tv"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:maxLength="@integer/max_length"
    />
在ArrayAdapter中

3):

int maxLength = getResources().getInteger(R.integer.max_length);

答案 2 :(得分:9)

从api 21你可以这样做:

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

我希望这有助于某人。

答案 3 :(得分:4)

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

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

答案 4 :(得分:2)

您可以使用Reflection 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
  • InputFilter上的每个TextView集进行迭代,找到一个属于InputFilter.LengthFilter实现的集。
  • 使用Reflection访问,获取并返回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,您应该将其作为无限长度处理。

答案 5 :(得分:1)

有点复杂,但我不知道任何其他方法。我希望它有效(未经测试):

XmlResourceParser parser = getResources().getLayout(R.layout.theLayout);
String namespace = "http://schemas.android.com/apk/res/android";
int maxLength = parser.getAttributeIntValue(namespace, "maxLength", 12);

答案 6 :(得分:0)

Kotlin一线解决方案-返回最大长度;如果未设置,则返回null

view.filters.filterIsInstance<InputFilter.LengthFilter>().firstOrNull()?.max

作为扩展名:

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