2014-12-11 1 views
0

데이터를 제출하기 전에 사용자 입력에 일련의 인증을 적용하고 있습니다. 각 검증은 실패 할 경우 사용자에게 대화 상자를 표시합니다.계속할 수있는 Android EditText 유효성 확인

if (check_1() == false) { 
    popup(MSG_1); 
    return false; 
} 
if (check_2() == false) { 
    popup(MSG_2); 
    return false; 
} 
... 

내 문제는 내가 아래 check_2 같이, 사용자가 재정의 할 수있는 검증 한 것인지입니다 :

if (check_1() == false) { 
    popup(MSG_1); 
    return false; 
} 
if (check_2() == false) { 
    boolean res = popup_with_override(MSG_2); // confirmation dialog 
    if (res == false) 
     return false; 
    // otherwise continue with the validation tests 
} 
if (check_3() == false) { 
    popup(MSG_3); 
    return false; 
} 
... 

나는 아래의 클래스와 같은 확인 대화 상자를 사용하려고하지만,하지 하나씩 검증 된 테스트 목록 아이디어에 잘 맞지 않습니다. 다른 검증에서 재정의 검증을 분리 같은 : 경고 대화 상자가 안드로이드에 차단되지 않기 때문에

public abstract static class DialogCallback implements DialogInterface.OnClickListener, Runnable { 
    public DialogCallback(Context c, String q) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(c); 
     builder.setMessage(q) 
       .setPositiveButton("Yes", this) 
       .setNegativeButton("No", this); 
     builder.create().show(); 
    } 

    public void onClick(DialogInterface dialog, int which) { 
     dialog.dismiss(); 
     if (which == DialogInterface.BUTTON_POSITIVE) { 
      mWhich = DialogInterface.BUTTON_POSITIVE; 
      run(); 
     } else { 
      mWhich = DialogInterface.BUTTON_NEGATIVE; 
      run(); 
     } 
    } 

    // 
    public int mWhich; 
} 

, 나는 접근 방식은 다시 할 수 있다고 생각?

아이디어가 있으십니까?

1) 내 체크 기능이 더 이상 부울 반환하지 않습니다 : 그것은 다른 사람을 도울 수 있다면

답변

0

마지막으로, 나는 나의 필요 (여러 재정의 검증을 통해 반복)을 해결하기 위해 다음과 같은 방법을 사용했다. 오류가 발생하는 경우 오류 메시지가 포함 된 클래스 인 List<ValidationError>, ValidationError, 오류가있는 경우 EditText (또는 전역 오류 인 경우 null) 및 부울 overridable (또는없는 오류)을 수집합니다. 따라서 확인 코드가된다 :

if (check_1() == false) { 
    // non-edittext specific error 
    ValidationErrors.add(new ValidationError(MSG_1, null, false)); 
} 
if (check_2() == false) { 
    // overridable error = warning 
    ValidationErrors.add(new ValidationError(MSG_2, editText2, true)); 
} 
if (check_3() == false) { 
    // edittext specific error 
    ValidationErrors.add(new ValidationError(MSG_3, editText3, false)); 
} 

2) 그런 다음 유효성 검사 방법은 실제 작업을 계속하거나 경고/사용자 중지 할 수 있습니다 : 마지막으로 ConfirmWarnings 방법이 그래서 재귀

void Validate() { 

    ArrayList<String> warnings = new ArrayList<String>(); 

    for (ValidationError ve : ValidationErrors) { 
     // global err : display & return 
     if (ve.editText == null && !ve.overridable) { 
      popup(ve.message); 
      return; 
     } 
     // local err : mark the edittext & return 
     if (ve.editText != null && !ve.overridable) { 
      ve.editText.setError(ve.message); 
      return; 
     } 
     // global warning : collect the message 
     if (ve.editText == null && ve.overridable) { 
      warnings.add(ve.message); 
     } 
     // local warning : mark the edittext & collect the message 
     if (ve.editText != null && ve.overridable) { 
      ve.editText.setError(ve.message); 
      warnings.add(ve.message); 
     } 
    } 

    // Confirm (or not) the warnings 
    ConfirmWarnings(warnings); 
} 

3) 다음과 같은 경우 모든 경고를 반복 할 수 있습니다.

void ConfirmWarnings(ArrayList<String> warns) { 
    if (warns.size() > 0) { 
     new DialogCallback(this, warns.get(0) + ", continue ?", tags) { 
      @override 
      public void run() { 
       if (this.mWhich == DialogInterface.BUTTON_POSITIVE) { 
        warns.remove(0); // consume the warnings as they are accepted 
        ConfirmWarnings(warns); 
       } 
      } 
     } 
    } 
    else 
     RealWork(); 
}