好久沒用過EditText限制長度了,今天用代碼設置都不知道該怎么用了,記錄下吧
一般我們在xml中使用maxLength來設置,如下:
<EditText
android:id="@+id/et_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="2"
android:textColor="@android:color/black"
android:textSize="16sp" />
但是你在代碼中會發現沒有setMaxLength這個方法,然而設置setEms這個還沒用,我曹了。然后看了下TextView源碼是這樣設置的
setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });
所以要是代碼限制長度就需要使用setFilters方法了
