2011-04-04 48 views
0

我是Android的初学者。 我有一个字母的EditText。它只接受字符。当用户在键盘上输入不同的字符时,我需要用新的字符替换输入的字符。我怎样才能达到目的?这可能吗?Android EditText

// 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); 

回答

0

考虑使用正则表达式和的replaceAll为:

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

这个答案可能会或可能不会帮你的。

+0

谢谢 Igor 2011-04-05 14:20:54

0

TextWatcher应该派上用场。

+0

谢谢,我已经执行beforeTextChanged – Igor 2011-04-05 14:21:55