2012-11-08 2 views
0

EditText가 텍스트가 아닌 숫자 만 얻도록 제한하고 싶습니다. 나는 이것을 시도 :EditText가 android에서 숫자를 얻지 못하도록 텍스트를 가져 오는 것을 제한하는 방법

1-- XML에서이 나를 위해 작동하지 않았다

 <EditText android:id="@+id/editText1" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:hint="@string/search_prompt" 
     android:inputType="text"/> 

. 자바 코드에서

2-- 참조 Text Only Input EditText android

myEditText.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL); 

을 가지고 있지만,이 또한 작동하지 않습니다.

답변

6

당신이하지 nummbers을받을 만 얻을 텍스트 글고 치기를 제한 할 InputFilter를 사용할 수 있습니다

InputFilter filtertxt = 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))) { 
          return ""; 
        } 
      } 
      return null; 
    } 
}; 

editxt.setFilters(new InputFilter[]{filtertxt}); 

편집 : 당신은 당신 또는 EDITTEXT에 의해 허용하려는 InputFilter 내부의 문자 배열을 만들 수 있습니다 또한 정규식 유효성 검사를

 InputFilter[] Textfilters = new InputFilter[1]; 
     Textfilters[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','.'}; 

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

     }; 
editxt.setFilters(Textfilters); 
+0

이것은 어떻게 작동합니까? EditText는','및 텍스트 만 가져야합니다. –

+0

@ArshadAliSoomro : edittext가 InputFilter에서 허용하는 모든 char을 사용하여 char [] 배열을 정의하십시오. –

+0

일부 코드를 제게 알려 주실 수 있습니까? –

4

을 사용할 수 있습니다 당신은 XML로 사용하는 것을 할 수

<EditText 
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" /> 
관련 문제