如何在代码中获取 EditText maxLength 设置 [英] How to Get EditText maxLength setting in code

查看:24
本文介绍了如何在代码中获取 EditText maxLength 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在运行时查看 EditTextmaxLength 以便能够做出文本显示决定.
这可能吗?

I would like to see the maxLength of an EditText at run time to be able to make a text display decision.
Is that possible?

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

Here is a description of what I wan't to do.

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

I have a ListView with many rows and each row have an EditText and a TextView.
I've made a subclass of ArrayAdapter to be able to feed the String that I want to place in the EditText of each row.
I have set android:maxLength="12" in the XML file.
I want to display a number in that EditText field, but if the number I want to display has more than android:maxLength="12" I want to display an "error message" instead.

而且我不希望在我的 ArrayAdapter 子类中硬编码这 12 个.

And I would prefer not to hard code that 12 in my subclass of ArrayAdapter.

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

There is probably a simple solution, but I haven't found it yet.
(android first time...)

推荐答案

只有有限的参数才有getter,所以我觉得你看不懂.

Only limited parameters have their getters, so I don't think you can read it .

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

So write length (Say 12) in values folder and use it in xml layout and arrayAdapter . Now its not hard-coded .

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"
    />

3) 在 ArrayAdapter 中:

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

这篇关于如何在代码中获取 EditText maxLength 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆