1 TextView的 android:textAllCaps=“true”
TextView上可以设置 android:textAllCaps="true"就可以把text中的小写字母展示成大写字母,如果想让EditText也可以把输入的小写字母转换成大写字母呢。
注意:
如果在EditText中添加android:textAllCaps=“true”,当在EditText中输入时会导致崩溃:
java.lang.IndexOutOfBoundsException
at android.view.GLES20Canvas.drawText(GLES20Canvas.java:888)
at android.text.Layout.drawText(Layout.java:367)
at android.widget.Editor.drawHardwareAccelerated(Editor.java:1514)
at android.widget.Editor.onDraw(Editor.java:1439)
at android.widget.TextView.onDraw(TextView.java:5780)
at android.view.View.draw(View.java:15488)
at android.view.View.updateDisplayListIfDirty(View.java:14377)
如何让输入的小写字母变成大写字母:
利用EditText的setTransformationMethod方法:
setTransformationMethod函数:
/**
* Sets the transformation that is applied to the text that this
* TextView is displaying.
*
* @attr ref android.R.styleable#TextView_password
* @attr ref android.R.styleable#TextView_singleLine
*/
public final void setTransformationMethod(TransformationMethod method) {
setTransformationMethod函数通过设置自定义的TransformationMethod可以把输入的字符映射成其他字符,需要知道的是它只是映射成其他字符,当从EditText取字符时还是原来的字符,类似password框,把输入的密码变成*,只是显示效果变了,真正取值时时没有变化的。
注意:setTransformationMethod方法只是修改了显示效果,真正取到的还是输入的值,如果输入了aaaa,显示AAAA,取到的值为aaaa,所以要真正转成大写,需要取到值之后再转成大写。
自定义TransformationMethod:
//把小写映射成大写
public static class UpperTransform extends ReplacementTransformationMethod {
@Override
protected char[] getOriginal() {
char[] ori = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
return ori;
}
@Override
protected char[] getReplacement() {
char[] dis = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
return dis;
}
}
//然后
inputDlg.setTransformationMethod(new UpperCaseTransform());
//注意取值时,需要将取到的EditText的值利用.trim().toUpperCase()转换成大写。
2 EditText限制输入的字符
android:digits=“0123456789abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ”
添加了这个属性,只能输入android:digits中有的字符,其他字符点击键盘也不会被输入到EditText中。