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

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

相关文档推荐

Prevent enter key on EditText but still show the text as multi-line(防止在 EditText 上输入键,但仍将文本显示为多行)
Android keyboard next button issue on EditText(EditText上的Android键盘下一个按钮问题)
When the soft keyboard appears, it makes my EditText field lose focus(当软键盘出现时,它使我的 EditText 字段失去焦点)
android how to make text in an edittext exactly fixed lines(android如何在edittext中制作文本完全固定的行)
How to use regular expression in Android(如何在 Android 中使用正则表达式)
Filter list view from edit text(从编辑文本中过滤列表视图)