在Android EditText中,如何强制写大写?

In Android EditText, how to force writing uppercase?(在Android EditText中,如何强制写大写?)
本文介绍了在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中,如何强制写大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)