問題描述
我希望在運行時查看 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模板網!