简体   繁体   English

Android EditText

[英]Android EditText

I am a beginner in Android. 我是Android的初学者。 I have an EditText for one letter. 我有一个字母的EditText。 It accepts only characters. 它仅接受字符。 I need to replace entered character with the new one when the user enters different character on the keyboard. 当用户在键盘上输入其他字符时,我需要用新字符替换输入的字符。 How can I achive that? 我该如何实现? Is this possible? 这可能吗?

// Set edit text width only to one character
    InputFilter[] FilterArray = new InputFilter[2];
    FilterArray[0] = new InputFilter.LengthFilter(1);
    FilterArray[1] = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; i++) {
                if (!Character.isLetter(source.charAt(i))
                        && !Character.isSpaceChar(source.charAt(i))) {
                    return "";
                }
            }
            return null;
        }
    };
    editLetter.setFilters(FilterArray);

Consider using regular expressions and replaceAll as in: 考虑使用正则表达式和replaceAll,如下所示:

// ASSERT inString not null
public static String removeTags(String inString) throws IllegalArgumentException {
    if (inString == null) {throw new IllegalArgumentException();}
    return inString.replaceAll("<.*>","");
}

This answer may or may not help you at all. 这个答案可能对您完全没有帮助。

TextWatcher应该可以派上用场。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM