Android中限制输入框最大输入长度

通常情况下只需要在布局文件中加入maxlength这一属性即可

 1   <EditText
 2                 android:inputType="text"
 3                 android:singleLine="true"
 4                 android:maxLength="16"
 5                 android:hint="请输入密码"
 6                 android:layout_width="match_parent"
 7                 android:layout_height="40sp"
 8                 android:background="@drawable/rounded_edittext"
 9                 android:textSize="20sp"
10                 android:layout_gravity="center_vertical"
11                 android:id="@+id/ed_password"/>

最近做项目的时候发现不灵了,上网查了一下,发现是因为设置了禁止输入空格和回车导致冲突,可以在逻辑代码中添加一下内容解决

1 //限制输入最大长度
2         ed_account.setFilters( new InputFilter[]{new InputFilter.LengthFilter(16)});

特此记录

原文地址:https://www.cnblogs.com/999625696397qq/p/10630505.html