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

How to Get EditText maxLength setting in code(如何在代码中获取 EditText maxLength 设置)
本文介绍了如何在代码中获取 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 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)
How can i use MYSQL database connection in iphone application useing cocos2d?(如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?)
Smoothly drag a Sprite in cocos2d - iPhone(在 cocos2d 中平滑拖动一个 Sprite - iPhone)
Cocos2D complex animations(Cocos2D 复杂动画)