Android actually has a built-in InputFilter just for this!
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
Be careful, setFilters
will reset all other attributes which were set via XML (i.e. maxLines
, inputType
,imeOptinos
...). To prevent this, add you Filter(s) to the already existing ones.
InputFilter[] editFilters = <EditText>.getFilters();
InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
newFilters[editFilters.length] = <YOUR_FILTER>;
<EditText>.setFilters(newFilters);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…