在Android EditText中,如何强制写大写? [英] In Android EditText, how to force writing uppercase?

查看:21
本文介绍了在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 设置的所有其他属性(即 maxLinesinputType,imeOptinos...).为防止这种情况发生,请将您的过滤器添加到现有的过滤器中.

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中,如何强制写大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆