2011-08-10 5 views

답변

0

다른 솔루션 중에서도 필드를 만들고 그 시간에 한 문자 만 허용하도록 이벤트 처리기를 설정할 수 있습니다.

이 그 질문에 매우 가까이 : Validation on Edit Text

2

당신은 허용되는 문자를 제한 할 수있는 텍스트 필드와 사용자 정의 InputFilter에 입력 할 수있는 문자의 수를 제한하는 InputFilter.LengthFilter을 사용할 수 있습니다; 그것은 나에게 가장 단순한 접근처럼 들린다.

EditText myTextField = (EditText) findViewById(R.id.my_text); 

InputFilter validCharsInputFilter = new InputFilter() { 

     @Override 
     public CharSequence filter(CharSequence source, int start, int end, 
       Spanned dest, int dstart, int dend) { 

      // Loop through characters being inserted 
      for (int i = start; i < end; i++) { 

       // If it is not a letter 
       if (!Character 
         .isLetter(source.charAt(i))) { 

        // Return empty string - as char not allowed 
        return ""; 
       } 
      } 

      // If we've got this far, then return null to accept string 
      return null; 
     } 
    }; 

myTextField.setFilters(
     new InputFilter[] { new InputFilter.LengthFilter(1), validCharsInputFilter }); 
: 여기

은 예입니다
관련 문제