本文介绍了在Android EditText中,如何强制大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中,我有不同的EditText,用户可以在其中输入信息.但是我需要强迫用户以大写字母书写.您知道执行此操作的功能吗?

In my Android application I have different EditText where the user can enter information. But I need to force user to write in uppercase letters.Do you know a function to do that?

推荐答案

Android实际上有一个为此内置了InputFilter

Android actually has a built-in InputFilter just for this!

edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});

请注意,setFilters 将重置通过XML设置的所有其他属性(即maxLinesinputTypeimeOptinos ...).为了防止这种情况,请将您的过滤器添加到已经存在的过滤器中.

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

这篇关于在Android EditText中,如何强制大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:27