简体   繁体   中英

Android EditText

I am a beginner in Android. I have an EditText for one letter. 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:

// 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应该可以派上用场。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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