如何获取code的EditText最大长度设置 [英] How to Get EditText maxLength setting in code

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

问题描述

我想看到的最大长度的EditText 在运行时能够使文本显示决定。
这可能吗?

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?

下面是我wan't做了说明。

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

我有很多行,每行一个ListView有一个EditText和一个TextView。
我做了ArrayAdapter的一个子类,能够养活,我想在每行的EditText把字符串。
我已经设置 Android版本:最大长度=12在XML文件中
。 我想显示一些在这的EditText领域,但如果我想显示的数量已经超过 Android版本:最大长度=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.

和我preFER不是要拼命code,在我的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...)

推荐答案

只有有限的参数都有自己的干将,所以我不认为你可以读取它。

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

所以写长度(说12)值的文件夹,并用它在XML布局和arrayAdapter。 现在,它不是硬codeD。

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

1)创建integer.xml价值观*

1)Create integer.xml in values *

<?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);

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

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