2012-10-09 3 views
0

필자는 문자를 편집 중에 ### ## 형식으로 만 입력 할 수 있도록 허용해야합니다.이를 달성 할 방법이 있습니까? 나는 코드 아래 사용했지만 그것은 목적을 제공하지 않습니다 :편집 중에 텍스트를 입력하는 시간을 제한하는 방법은 무엇입니까?

edit.setInputType(InputType.TYPE_DATETIME_VARIATION_TIME) 

을하지만 어떤 알파벳이 또한 잘 것 같은 입력 값 67과 같이 할 수 있습니다 : 나는 12 만하면 344,444 ... : 59 (최대) 형식, 콜론 최대 값을 입력 할 수 있다는 의미는 12이며 콜론 max는 59 이상일 수 없습니다.

어떻게 구현합니까?

참고 : 여기서는 텍스트 편집을 사용하고 사용자가 시간 값을 입력 할 수 있기 때문에 TimePicker 클래스를 사용하지 않을 것입니다.

하시기 바랍니다.

+0

어떻게 5 자리로 글고 제한에 대해? – Thommy

+0

2 개의 EditText가 각각에 대한 제한을 선언하고 사후 처리에서 값을 결합합니다. 이렇게하면':'을 삽입하고 사용자가 키보드에서 그것을 찾지 못하게 할 수 있습니까? – jnthnjns

+0

은 http://developer.android.com/reference/java/util/regex/Pattern.html 일 수 있습니다. –

답변

1

사용자 입력의 제어를 위해 사용 InputFilter :

여기
EditText editText; 
    editText.setFilters(new InputFilter[] { new InputFilter() { 
     @Override 
     public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
      // here you can evaluate user input if it's correct or not 
     } 
    } }); 

filter 메소드 구현 가능하지만,이 테스트되지 않습니다

  if (source.length() == 0) { 
       return null;//deleting, keep original editing 
      } 
      String result = ""; 
      result.concat(dest.toString().substring(0, dstart)); 
      result.concat(source.toString().substring(start, end)); 
      result.concat(dest.toString().substring(dend, dest.length())); 

      if (result.length() > 5) { 
       return "";// do not allow this edit 
      } 
      boolean allowEdit = true; 
      char c; 
      if (result.length() > 0) { 
       c = result.charAt(0); 
       allowEdit &= (c >= '0' && c <= '2'); 
      } 
      if (result.length() > 1) { 
       c = result.charAt(1); 
       allowEdit &= (c >= '0' && c <= '9'); 
      } 
      if (result.length() > 2) { 
       c = result.charAt(2); 
       allowEdit &= (c == ':'); 
      } 
      if (result.length() > 3) { 
       c = result.charAt(3); 
       allowEdit &= (c >= '0' && c <= '5'); 
      } 
      if (result.length() > 4) { 
       c = result.charAt(4); 
       allowEdit &= (c >= '0' && c <= '9'); 
      } 
      return allowEdit ? null : ""; 
관련 문제