首先,新建一个类继承ReplacementTransformationMethod,重写getOriginal()和getReplacement():
public class AlphabetReplaceMethod extends ReplacementTransformationMethod {
@Override
protected char[] getOriginal() {
return new char[]{'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'};
}
@Override
protected char[] getReplacement() {
return new char[]{'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'};
}
}
然后在 java 代码中设置 EditText 的setTransformationMethod(TransformationMethod method)方法:
mEditText.setTransformationMethod(new AlphabetReplaceMethod());