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)});

特此記錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM