通常情況下只需要在布局文件中加入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)});
特此記錄