EditText, used as an input box in Android apps, I want to limit the input to only uppercase letters! For iOS, refer to [iOS] Alphabet capitalization limit and length limit in UITextField.
Host: Windows 10 Android Studio: 3.3.2 Java
Open the layout, select the desired UITextField, and open the attributes: digits in the right pane for editing.
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Filter preparation
MyActivity.java
//Alphabet filter
private InputFilter alphabetFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if (source.toString().matches("^[a-zA-Z]+$")) {
return source.toString().toUpperCase(Locale.ROOT);
} else {
return "";
}
}
};
MyActivity.java
InputFilter myFilters[] = {alphabetFilter, new InputFilter.LengthFilter(5)}; //Uppercase + alphabet filter + length filter
EditText targetEditText = findViewById(R.id.edittext_target);
targetEditText.setFilters(myFilters);
You cannot enter numbers with the keyboard, the length is limited, Make sure that if you enter lowercase letters, they will be automatically converted to uppercase.
Thank you for your hard work!
Recommended Posts