2012-10-25 31 views
1

我有一個編輯文本獲取名稱作爲用戶輸入。我需要限制除點(。)以外的所有特殊字符。這個怎麼做?請參閱下編輯名稱文本

EditText Name= new EditText(this); 
Name.setLayoutParams(new TableRow.LayoutParams(dp(220),dp(40))); 
Name.setHorizontallyScrolling(true);   
Name.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); 
Name.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME); 
Name.setTypeface(Typeface.DEFAULT); 
Name.setFilters(new InputFilter[]{new InputFilter.AllCaps()}); 
+0

http://www.mkyong.com/regular-expressions/how-to-validate-username-見本with-regular-expression/ –

+0

使用正則表達式與模式匹配。 – Raghunandan

+2

它只是XML中的一行 - inputType =「textPersonName | name」 – Shark

回答

2

試試這個

EditText editText = (EditText)findViewById(R.id.editText); 
     InputFilter[] filters = new InputFilter[1]; 
     filters[0] = new InputFilter(){ 
      public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
       if (end > start) { 

        char[] acceptedChars = 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', 
          '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', 
          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.'}; 

        for (int index = start; index < end; index++) {           
         if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) { 
          return ""; 
         }    
        } 
       } 
       return null; 
      } 

     }; 
     editText.setFilters(filters); 
+0

非常感謝,看起來很棒 – shivcena

+0

很高興幫助你,謝謝你接受我的回答 –

3

使用inputType這裏我的代碼的鏈接,文檔是指它

inputType="textPersonName" 

http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType 使用

NameEdt.setRawInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME); 
+0

我需要限制@ $ /等特殊字符;等,但它會允許如果我使用textPersonName,我喜歡NameEdt.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME | InputType.TYPE_CLASS_TEXT); – shivcena

+0

已更新的答案檢查代碼 –

1
InputFilter[] filters = new InputFilter[1]; 
    filters[0] = new InputFilter(){ 
     @Override 
     public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
      if (end > start) { 

       char[] acceptedChars = new char[]{'a','b'}; 

       for (int index = start; index < end; index++) {           
        if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) { 
         return ""; 
        }    
       } 
      } 
      return null; 
     } 

    }; 
    Name.setFilters(filters); 
+0

非常感謝,我按照上面的代碼,它工作得很好 – shivcena

+0

@ user1500635最受歡迎的朋友...... :) –

1

NumberKeyListener PwdkeyListener =新NumberKeyListener(){

public int getInputType() { 
return InputType.TYPE_MASK_VARIATION; 
} 

    @Override 
    protected char[] getAcceptedChars() { 
    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', 
        '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', 
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '@', '_', '#', '$', '%', '&', '*', '-', '+', '(', ')', '!', '"', '\'', ':', 
        ';', '/', '?', ',', '~', '`', '|', '\\', '^', '<', '>', '{', '}', '[', ']', '=', '£', '¥', '€', '¢', '•','©' }; 
    } 
}; 

edtObj.setKeyListener(PwdkeyListener); 

更多信息Android - Want to restrict some charaters to the edittext

+0

非常感謝,我遵循上面的代碼,它工作得很好 – shivcena