我希望在运行时查看 EditText 的 maxLength 以便能够做出文本显示决定.
这可能吗?
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 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
EditText:禁用文本选择处理程序单击事件上的粘贴EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本选择处理程序单击事件上的粘贴/替换菜
2.3 上带有完成 SoftInput 操作标签的多行 EditTextMultiline EditText with Done SoftInput Action Label on 2.3(2.3 上带有完成 SoftInput 操作标签的多行 EditText)
如何在 Android 中检测向左或向右滑动?How to detect the swipe left or Right in Android?(如何在 Android 中检测向左或向右滑动?)
防止在Android中的屏幕旋转对话框解除Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋转对话框解除)
如何处理 ImeOptions 的完成按钮点击?How do I handle ImeOptions#39; done button click?(如何处理 ImeOptions 的完成按钮点击?)
您如何将 EditText 设置为仅接受 Android 中的数值How do you set EditText to only accept numeric values in Android?(您如何将 EditText 设置为仅接受 Android 中的数值?)