EditText动态设置长度限制

好久没用过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方法了

posted @ 2021-05-14 16:44  拜雨  阅读(1059)  评论(0编辑  收藏  举报